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

Accumulate models with extra properties

Hello Phalcons!

I try to accumulate some models with extra properties for the response like this:

            $todo = Todos::findFirstById($id);
            $todo->user = $todo->user; // aliased relationship
            $todo->users = $todo->getUsers()->toArray();
            $todo->contacts = $todo->getContacts()->toArray();
            $todo->projects = $todo->getProjects()->toArray();
            $todo->locations = $todo->getLocations()->toArray();
            $todo->documents = $todo->getDocuments()->toArray();
            return $this->response->setContent($todo);

All relations are set up properly and the data is fetched and returned correctly.

My model instance ($todo) does accumulate only a few of this properties, some are stored in the _related Array of the instance but are not exposed as $todo->projects etc.

Why is this? I tried to figure out if this has something todo with alias configurations but no matter how I name this properties (e.g. $todo->relatedProjects) it only goes in the _related array as the property name given.

So it might help if I can somehow query the _related array of a model instance but found no way to achieve this. Can someone explain this behaviour?



98.9k

Currently _related is a protected property in the model, you can add a method to the model to get that value:

<?php

class Robots
{
    public function getInternalRelated()
    {
        return $this->_related;
    }
}