We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model custom setter don't work when trying to set array

I create custom setter for Model property:

class Test extends Model {

    protected $restrictions;

    public function setRestrictions ($value) {
        die('setter called');
    }

}

In controller I try to set array to property:

$model->restrictions = [];
// setter wasnt called 

I need to set array and implement my custom logic in setter.

edited Apr '16

You have assigned value to the property. I don't think ORM will call setter by default, unless you call it directly as a method which will contain your specific logic.

https://docs.phalcon.io/en/latest/reference/models.html#creating-updating-records



3.7k

I don't think ORM will call setter by default

This works, when I try to set string instead of array:

$model->restrictions = 'test';
//setter was called

Can you post your full code ? $model->restrictions = []; This is only one assigment you are doing ? There is currently bug on phalcon(or intentional behaviour). Where once you will set some property then it will execute setter but then it will not.



3.7k

Can you post your full code ?

It's full code.

$model->restrictions = []; This is only one assigment you are doing ?

Yes.

So, model custom setter doesn't called when I try to set array. String, int, object is working fine.



145.0k
Accepted
answer

This is happening beacause this code is excuted:

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep#L4115

Why you want to access not accessible property anyway ? Just use setter. Phalcons model class has his own magic __set method which is causing problems when accessing non accessible properties.



3.7k

Why you want to access not accessible property anyway ?

Becouse in model I have public and protected properties. Some properties can be setted directly witout setter and some need setter with some logic. I like to do this in one coding style:

class Test extends Model {

    public $name;
    protected $restrictions;

    public function setRestrictions ($value) {
        //..
    }

}

// setting without setter
$model->name = 'name';
// setting with setter
$model->restrictions = [];
edited May '16

You not setting with setter. You are using non-accesible property and php is calling magic method. You need to use setter:

$model->setRestrictions([])



3.7k

You not setting with setter. You are using non-accesible property and php is calling magic method. You need to use setter:

$model->setRestrictions([])

Yes, I'm using setter, but I'll want to have accessibility to use non-accesible property, but phalcon setter isn't correct with arrays.

edited May '16

But why ? It's wrong and you shouldn't do it. You are not using SETTER HERE:

$model->restrictions = [];

You are accessing here non-accessible property. Setter is not called after that. There is first magic method __set called and then your setter should be called(but it's beacause phalcon expects relations as array). Just use setters, they are for this. Or just se assign/new model construcotr in 2.1.x for assigning properties in models.



3.7k

Or just se assign/new model construcotr in 2.1.x for assigning properties in models.

How to use this?

If you are creating model in 2.0.x/editing existing then:

$model->assign([
    'name'=>'name',
    'restrictions'=>[]
]);

If you are creating new model in 2.1.x then it's much simpler:

$model = new SomeModel([
    'name'=>'name',
    'restrictions'=>[]
]);