Get array of models before the model is created

Hi there,

I have a model set up with a class robot {public function initialize() $this->hasMany("id", "robotparts", robotpart_id, ["alias => "robotparts"); }

Before creating a robot model, I set the robotparts variable $robot = new Robot(); $robot->robotparts = Array of robotparts. $robot->save() The model is created correctly with all the robot parts in one transction.

However, If before creating I would like to get an array of previously assign robotparts array to the $robot model, I get errors.

The question is, how to get $robot->robotparts array before $robot->create()

Thank you!

try with this code and tell us how it went

class robot {
    public function initialize() {
        $this->hasMany("id", "robotparts", "robotpart_id", ["alias => "robotparts"); 

    public function getParts() {
        return $this->robotparts;

    public function setParts(array $parts) {
        $this->robotparts = $parts; // check that they are from the robotsparts class

        return $this;

Good luck


Thanks for this example. I get the parts via getParts() method only after the model is saved.


But if I don't save the model $robot, I can't get the parts via getParts() method.

with your model? with my example either?


With your example I get the same problem.

I had to write a wrapper to access the models array before the the model is saved. However, I believe this should work with standard models... Can I add somwhere this as a feature that must be included in the next phalcon release?

<?php use Phalcon\Mvc\Model;

class RelatedWrapper extends Model { private $related_tables = [];

public function setRelatedBeforeCreate($alias, $records)
    $this->related_tables[$alias] = $records;
    $this->$alias = $records;

public function getRelatedArray($table_alias) {
    if(array_key_exists($table_alias, $this->related_tables)) {
        return $this->related_tables[$table_alias];
    } else {
        return $this->$table_alias;