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.

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 jsonencode 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 jsonencode(Robots::findFirstById(10)) ? How i can resolve this problem with a better way?

Thanks



7.6k
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.phalconphp.com/en/latest/reference/models.html#model-resultsets