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) {
            $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.

1 Answer 1


You have to do:

$matrixBlock->getContent()->setAttribute('secondLightswitch', '1');

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

  • Thanks Robin - now I need to read up about "magic methods". Apr 6, 2018 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.