<p>I am working on a plugin for Craft 3 that will add new matrix data to users profile and keep the old/current data that is in the matrix field.</p>

<p>I have been looking at these but they are for Craft 2 so maybe that's the problem:</p>

<ul>
<li><p><a href="https://craftcms.stackexchange.com/questions/9996/updating-user-profile-matrix-block-with-plugin">Updating user profile matrix block with plugin</a></p></li>
<li><p><a href="https://craftcms.stackexchange.com/questions/9054/creating-a-matrix-entry-with-a-plugin?rq=1">Creating a matrix entry with a plugin</a></p></li>
</ul>

<p>The below is what I have tried but getting the error "Class 'controllers\MatrixBlockModel' <strong>not found</strong>"</p>

<pre><code>$member_id = Craft::$app-&gt;getUser()-&gt;getId();
$dateCreated = date('Y-m-d H:i:s');

$block = new MatrixBlockModel();
$block-&gt;fieldId = 77;
$block-&gt;ownerId = $member_id; //Users ID
$block-&gt;typeId = 11; //Block Types ID
$block-&gt;ownerLocale = 'en_us';
$block-&gt;sortOrder = 1;

$block-&gt;getContent()-&gt;setAttributes(array(
    'fileDownloaded' =&gt; 'fileDownloadedxxxxprotectedlinks_links',
    'manufacturer' =&gt; 'manufacturerxxxxprotectedlinks_links',
    'date' =&gt;  $dateCreated,
));

$success = Craft::$app-&gt;matrix-&gt;saveBlock($block);
</code></pre>

<p>I also added:</p>

<pre><code>use craft\elements\MatrixBlock;
</code></pre>

<p>Any ideas?</p>

CC BY-SA 4.0
2018-06-07T21:51:08.400Z
I have a feeling your namespace is off, brush up here and see if anything is wrong http://php.net/manual/en/language.namespaces.php
Clark Nelson
CC BY-SA 4.0
2018-06-09T09:10:49.790Z
As Clark already said: `MatrixBlockModel` is the wrong class, you want `new craft\elements\MatrixBlock()`
Robin Schambach
<plugin-development>
Updating user profile matrix block with plugin (Craft 3)
# Updating user profile matrix block with plugin (Craft 3)

(Asked by: username_0 on 2018-06-07)

I am working on a plugin for Craft 3 that will add new matrix data to users profile and keep the old/current data that is in the matrix field.
I have been looking at these but they are for Craft 2 so maybe that's the problem:

Updating user profile matrix block with plugin
Creating a matrix entry with a plugin

The below is what I have tried but getting the error "Class 'controllers\MatrixBlockModel' not found"
$member_id = Craft::$app->getUser()->getId();
$dateCreated = date('Y-m-d H:i:s');

$block = new MatrixBlockModel();
$block->fieldId = 77;
$block->ownerId = $member_id; //Users ID
$block->typeId = 11; //Block Types ID
$block->ownerLocale = 'en_us';
$block->sortOrder = 1;

$block->getContent()->setAttributes(array(
    'fileDownloaded' => 'fileDownloadedxxxxprotectedlinks_links',
    'manufacturer' => 'manufacturerxxxxprotectedlinks_links',
    'date' =>  $dateCreated,
));

$success = Craft::$app->matrix->saveBlock($block);

I also added:
use craft\elements\MatrixBlock;

Any ideas?

**Tags:** plugin-development

> **Comments:**
> **username_1**: I have a feeling your namespace is off, brush up here and see if anything is wrong http://php.net/manual/en/language.namespaces.php  
> **username_2**: As Clark already said: `MatrixBlockModel` is the wrong class, you want `new craft\elements\MatrixBlock()`  

---

## 0 answers

