<p>You have to do: </p>

<pre><code>$matrixBlock-&gt;getContent()-&gt;setAttribute('secondLightswitch', '1');
</code></pre>

<p>You can only <strong>get</strong> attributes via magic methods but you can't <strong>set</strong> them</p>

CC BY-SA 3.0
2018-04-06T08:03:32.737Z
Thanks Robin - now I need to read up about "magic methods".
Matt Wilcox
CC BY-SA 3.0
2018-04-05T14:09:22.763Z
2018-04-05T14:09:22.763Z
Robin Schambach
<p>I have a plugin which is used to send a notification email when certain events happen.</p>

<p>I need to watch the contents of a Matrix field attached to a User, and when a Lightswitch field inside the Matrix is 'on', flip another Lightswitch field on the same matrix block. Here's the simplified code I have so far:</p>

<pre><code>craft()-&gt;on('users.onBeforeSaveUser', function(Event $event) {
    foreach ($user-&gt;getContent()-&gt;myMatrixField as $matrixBlock) {
        if($matrixBlock-&gt;firstLightswitch){
            $matrixBlock-&gt;secondLightswitch = '1';
        }
    }
}
</code></pre>

<p>I am getting the following error though:</p>

<blockquote>
  <p>Property “Craft\MatrixBlockModel.secondLightswitch” is not defined</p>
</blockquote>

<p>It's very definitely defined. I presume I'm trying to set the field incorrectly, but can't find out how.</p>

<plugin-development><matrix><lightswitch>
Amending a lightswitch field value inside a matrix, from a plugin?
# Amending a lightswitch field value inside a matrix, from a plugin?

(Asked by: username_0 on 2018-04-05)

I have a plugin which is used to send a notification email when certain events happen.
I need to watch the contents of a Matrix field attached to a User, and when a Lightswitch field inside the Matrix is 'on', flip another Lightswitch field on the same matrix block. Here's the simplified code I have so far:
craft()->on('users.onBeforeSaveUser', function(Event $event) {
    foreach ($user->getContent()->myMatrixField as $matrixBlock) {
        if($matrixBlock->firstLightswitch){
            $matrixBlock->secondLightswitch = '1';
        }
    }
}

I am getting the following error though:

Property “Craft\MatrixBlockModel.secondLightswitch” is not defined

It's very definitely defined. I presume I'm trying to set the field incorrectly, but can't find out how.

**Tags:** plugin-development, matrix, lightswitch


---

## 1 answers

### Answer by username_1 on 2018-04-05. Score: 4 | Accepted answer

You have to do: 
$matrixBlock->getContent()->setAttribute('secondLightswitch', '1');

You can only get attributes via magic methods but you can't set them

> **Comments:**
> **username_0**: Thanks Robin - now I need to read up about "magic methods".  

---

