Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-06T08:03:32.737Z
Answers.Text: Thanks Robin - now I need to read up about "magic methods".
Answers.UserDisplayName: Matt Wilcox
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-05T14:09:22.763Z
Answers.LastActivityDate: 2018-04-05T14:09:22.763Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Tags: <plugin-development><matrix><lightswitch>
Title: Amending a lightswitch field value inside a matrix, from a plugin?
ThreadText: # 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 username_1 - now I need to read up about "magic methods".  
  
  ---
  
  