Create new entity from array with Phalcon Framework

320 views Asked by At

Is it possible to set Entities values automatically from array?
my entity inherets from \Phalcon\Mvc\Model and I want to set all values from array instead of querying database
is this feature already implemented?

2

There are 2 answers

1
jodator On

Yes you can!

$myModel = new MyModel();
$myModel->save(['name' => 'Jeff']);
// even from post data, second argument is a whitelist of fields
$myModel->save($_POST, ['name']);

// The same for updating
$myModel = MyModel::findFirst();
$myModel->update(['name' => 'jodator']);

More here: Creating and updating records

0
Piotr On
$MyModel= new MyModel();

$MyModel->assign(array(
   'id' => 1,
   'name' => 'y'
));

//or

$MyModel->id = 1;
$MyModel->name = 'y';