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

Problem with Resultset + json_encode

Hi, I am beginner in Phalcon and i have a problem with model resultset, how i can force a return relations objects of my model without manual access of sub models? Example:

$myModel = Robots::findFirstById(10); $myModel->robotsParts->Parts->name; //I cannot do this;

If i return this model to json_encode i don't receive a $myModel->robotsParts->Parts without a above access of object! I can configure to ResultSet for return all sub relations in a simple json_encode(Robots::findFirstById(10)) ? How i can resolve this problem with a better way?

Thanks



13.0k
Accepted
answer
edited Feb '16

hi

in my opinion

controller

      $myModel = Robots::findFirst(10);

          foreach ($myModel->robotsParts as $value) {

                $arrayname  []= $value->parts->name;
          }

        print_r($arrayname);

robots model


public function initialize()
    {
        $this->hasMany("id", "RobotsParts", "parts_id");
    }

for Resultsets u can use find

https://docs.phalcon.io/en/latest/reference/models.html#model-resultsets