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

How to pass parameters in the constructor instance of Phalcon\Mvc\Model

The class extends Phalcon\Mvc\Model, I want to pass two params in the construct method, But Phalcon\Mvc\Model construct is final. How can i do that?

Since it's final I can think only of writing a setter method an passing the parameters there.



10.5k
Accepted
answer
edited Mar '14

@Gregor Panek is right. But already we have existing assign (setter) method like below::

$modelInstance->assign(array(
    'key1' => 'value1',
    'key2' => 'value2',
    'year' => 2014,
    [...]
));


31.9k

Thanks a6oozar Again..It look like just this only way

Dear @gl-liao in fact we have two other ways too like below:

$robot = \Phalcon\Mvc\Model::cloneResultMap(new Robots(), array(
         'type' => 'mechanical',
         'name' => 'Astro Boy',
         'year' => 1952
));

// OR

$robot = \Phalcon\Mvc\Model::cloneResult(new Robots(), array(
         'type' => 'mechanical',
         'name' => 'Astro Boy',
         'year' => 1952
));

There is any effort to support parameters on Model instantiation in futher versions?

OOP defines that constructor method should require necessary data to properly instantiate a new object allowing it to get a valid object state.

yes, would be nice if phalcon would support constructor params in future, models are kinda limited by design now

++

I would sort of think the initialize() method would take paramaters which would be bound to the model instance?