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

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

In case https://docs.phalcon.io/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?



98.9k
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();


1.1k

Great. Thanks a lot. ;-)