<p>I believe this is the correct way to get an existing Matrix block. Found this gem after digging around in the Google group.</p>

<pre><code>$block = craft()-&gt;matrix-&gt;getBlockById(blockId);
</code></pre>

<p>I updated my plugin code above to this and it seems to work properly.</p>

<pre><code>// THIS DOESN'T WORK
// $block = new MatrixBlockModel($post['kidId']);

// THIS WORKS
$block = craft()-&gt;matrix-&gt;getBlockById($post['kidId']);

$block-&gt;fieldId    = $kidsMatrix-&gt;id; // Matrix field's ID, 'Kids'
$block-&gt;ownerId    = $currentUser-&gt;id; // ID of entry the block should be added to
$block-&gt;typeId     = $kidBlock-&gt;id; // ID of block type, 'kid'
$block-&gt;getContent()-&gt;setAttributes(array(
    'wooly' =&gt; $post['myWooly'],
));

$response = craft()-&gt;matrix-&gt;saveBlock($block);
</code></pre>

CC BY-SA 3.0
2015-06-05T16:21:55.777Z
2015-06-05T17:13:35.587Z
2015-06-05T17:13:35.587Z
Roger Glenn
<p>I've written a plugin to update some user profile fields via ajax. It works great for normal text fields like this 'myWooly' field, ex:</p>

<pre><code>&lt;?php
namespace Craft;

class ProfileUpdateController extends BaseController
{
    protected $allowAnonymous = true;

    /**
     * updateWooly
     */
    public function actionUpdateWooly()
    {
        $this-&gt;requireAjaxRequest();

        $post = craft()-&gt;request-&gt;getPost();

        // get current user's profile info
        $currentUser = craft()-&gt;userSession-&gt;getUser();
        $profile = $currentUser-&gt;getContent();

        $currentUser-&gt;getContent()-&gt;myWooly = $post['myWooly'];

        if (craft()-&gt;users-&gt;saveUser($currentUser)) {
            $response = 'success';
        }
        else {
            $response = 'error';
        }

        $this-&gt;returnJson($response);
    }
</code></pre>

<p>I've created a Matrix field with each block having its own Wooly, ex:</p>

<pre><code>Kids (Matrix field)
 - kid (block type)
  + firstName
  + wooly
 - kid
  + firstName
  + wooly
 - etc. 
</code></pre>

<p>...and now I need for users to update the 'wooly' within each Matrix block.</p>

<p>I've updated my AJAX call to POST a Matrix Block ID and updated my actionUpdateWooly() action to the following:</p>

<pre><code>    public function actionUpdateWooly()
    {
        $this-&gt;requireAjaxRequest();

        $post = craft()-&gt;request-&gt;getPost();

        // get current user's profile info
        $currentUser = craft()-&gt;userSession-&gt;getUser();
        $profile = $currentUser-&gt;getContent();

        // SAVE WOOLY IN KIDS MATRIX FIELD
        if (isset($post['kidId'])) {

            $kidsMatrix = craft()-&gt;fields-&gt;getFieldByHandle("kids");

            $kidsMatrixBlocks = craft()-&gt;matrix-&gt;getBlockTypesByFieldId($kidsMatrix-&gt;id);
            foreach ($kidsMatrixBlocks as $key =&gt; $data) {
                if ($data-&gt;handle === "kid") {
                    $kidBlock = $data;
                    break;
                };
            };

            $block = new MatrixBlockModel($post['kidId']);
            $block-&gt;fieldId    = $kidsMatrix-&gt;id; // Matrix field's ID, 'Kids'
            $block-&gt;ownerId    = $currentUser-&gt;id; // ID of entry the block should be added to
            $block-&gt;typeId     = $kidBlock-&gt;id; // ID of block type, 'kid'
            $block-&gt;getContent()-&gt;setAttributes(array(
                'wooly' =&gt; $post['myWooly'],
            ));

            $response = craft()-&gt;matrix-&gt;saveBlock($block);

        }

        // WOOLY STORED AT TOP-LEVEL OF ACCOUNT
        else {
            $currentUser-&gt;getContent()-&gt;myWooly = $post['myWooly'];
        }

        if (craft()-&gt;users-&gt;saveUser($currentUser)) {
            $response = 'success';
        }
        else {
            $response = 'error';
        }

        $this-&gt;returnJson($response);
    }
</code></pre>

<p>This is successfully CREATING NEW matrix blocks in the correct field, but it's not UPDATING the one I'm expecting.</p>

<p>I would expect this to get the Matrix block by ID but apparently it's not:</p>

<pre><code>$block = new MatrixBlockModel($post['kidId']);
</code></pre>

<p>Yes, I've made sure the Matrix Block ID </p>

<p>Can anybody spot what I should be doing differently to get a specific matrix block and updated it within a user profile?</p>

<matrix><plugins><ajax><profile>
Updating user profile matrix block with plugin
# Updating user profile matrix block with plugin

(Asked by: username_0 on 2015-06-01)

I've written a plugin to update some user profile fields via ajax. It works great for normal text fields like this 'myWooly' field, ex:
requireAjaxRequest();

        $post = craft()->request->getPost();

        // get current user's profile info
        $currentUser = craft()->userSession->getUser();
        $profile = $currentUser->getContent();

        $currentUser->getContent()->myWooly = $post['myWooly'];

        if (craft()->users->saveUser($currentUser)) {
            $response = 'success';
        }
        else {
            $response = 'error';
        }

        $this->returnJson($response);
    }

I've created a Matrix field with each block having its own Wooly, ex:
Kids (Matrix field)
 - kid (block type)
  + firstName
  + wooly
 - kid
  + firstName
  + wooly
 - etc. 

...and now I need for users to update the 'wooly' within each Matrix block.
I've updated my AJAX call to POST a Matrix Block ID and updated my actionUpdateWooly() action to the following:
    public function actionUpdateWooly()
    {
        $this->requireAjaxRequest();

        $post = craft()->request->getPost();

        // get current user's profile info
        $currentUser = craft()->userSession->getUser();
        $profile = $currentUser->getContent();

        // SAVE WOOLY IN KIDS MATRIX FIELD
        if (isset($post['kidId'])) {

            $kidsMatrix = craft()->fields->getFieldByHandle("kids");

            $kidsMatrixBlocks = craft()->matrix->getBlockTypesByFieldId($kidsMatrix->id);
            foreach ($kidsMatrixBlocks as $key => $data) {
                if ($data->handle === "kid") {
                    $kidBlock = $data;
                    break;
                };
            };

            $block = new MatrixBlockModel($post['kidId']);
            $block->fieldId    = $kidsMatrix->id; // Matrix field's ID, 'Kids'
            $block->ownerId    = $currentUser->id; // ID of entry the block should be added to
            $block->typeId     = $kidBlock->id; // ID of block type, 'kid'
            $block->getContent()->setAttributes(array(
                'wooly' => $post['myWooly'],
            ));

            $response = craft()->matrix->saveBlock($block);

        }

        // WOOLY STORED AT TOP-LEVEL OF ACCOUNT
        else {
            $currentUser->getContent()->myWooly = $post['myWooly'];
        }

        if (craft()->users->saveUser($currentUser)) {
            $response = 'success';
        }
        else {
            $response = 'error';
        }

        $this->returnJson($response);
    }

This is successfully CREATING NEW matrix blocks in the correct field, but it's not UPDATING the one I'm expecting.
I would expect this to get the Matrix block by ID but apparently it's not:
$block = new MatrixBlockModel($post['kidId']);

Yes, I've made sure the Matrix Block ID 
Can anybody spot what I should be doing differently to get a specific matrix block and updated it within a user profile?

**Tags:** matrix, plugins, ajax, profile

---

## 1 answers

### Answer by username_0 on 2015-06-05. Score: 2

I believe this is the correct way to get an existing Matrix block. Found this gem after digging around in the Google group.
$block = craft()->matrix->getBlockById(blockId);

I updated my plugin code above to this and it seems to work properly.
// THIS DOESN'T WORK
// $block = new MatrixBlockModel($post['kidId']);

// THIS WORKS
$block = craft()->matrix->getBlockById($post['kidId']);

$block->fieldId    = $kidsMatrix->id; // Matrix field's ID, 'Kids'
$block->ownerId    = $currentUser->id; // ID of entry the block should be added to
$block->typeId     = $kidBlock->id; // ID of block type, 'kid'
$block->getContent()->setAttributes(array(
    'wooly' => $post['myWooly'],
));

$response = craft()->matrix->saveBlock($block);


---

