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

Add related record

Hi all,

I have an object, Request, with a has-many relationship to RequestItem (aliased to "items). So Request->items is a Simple Resultset.

I see in the docs there is a way to update related records with update(), and delete related records with delete(). Is there any way to add related records in a similar fashion?

I basically want to simply add a new related record to Request, and have Request->items reflect the addition. I was unable to find anything in the docs, which surprised me actually.

It's not clear (to me at least) on that page whether either of those examples will replace existing "Albums", or just append.

edited Mar '16

I'd like to bump this question becouse it's a little bit unclear to me how phalcon handles related records. Example in documentation says how to create new album with songs, but what when i'd like to append one when some of them already exist?

Im trying to implement this function:

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('id', 'Leave', 'userId', array('alias' => 'Leaves'));
    }

    /**
     * @param Leave $leave
     * @return bool
     */
    public function addLeave(Leave $leave)
    {
        if ($leave->getLeaveWorkingDays() <= $this->getAvailableLeaveDays()) {
            $this->leaves[] = $leave;
            return true;
        }
        return false;
    }

What I get is: Phalcon\Mvc\Model\Exception: Cursor is an immutable ArrayAccess object

I'm really curious about this as well.