Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Is there a setRelated() like method on a model object?

In case http://docs.phalconphp.com/en/latest/reference/models.html#implicit-transactions, it save all related objects with one save() called. But I use namespaces in models, It can't get or set related objects by magic method. There is a method named getRelated that can get related objects. oppositely , is there a setRelated method and works with the save() method?



81.2k
Accepted
answer

You can alias the relations to make them more friendly to work with:

<?php

namespace Store\Models;

class Robots extends Phalcon\Mvc\Model
{
    public funciton initialize()
    {
        $this->hasMany('id', 'Store\Models\RobotsParts', 'robots_id', array(
            'alias' => 'robotsParts'
        ));
    }          
}

Then, you can use the alias to get/set related records:

$robotParts = $robot->robotParts;
$robotParts = $robot->getRobotParts();


586

Great. Thanks a lot. ;-)