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.

Model with relationships

I copied an example in phalcon document to create model relationship. Then I tried to print out the result.

public function test(){
        $robots = Robots::findFirst();
        $parts = Parts::findFirst();
        $robotsparts = RobotsParts::findFirst();
        echo '<pre>' . print_r($robots->toArray(),true) . '</pre>';
        echo '<pre>' . print_r($parts->toArray(),true) . '</pre>';
        echo '<pre>' . print_r($robotsparts->toArray(),true) . '</pre>';
}

This is what I got:

<pre>Array
(
    [id] => 1
    [name] => Robot1
    [type] => Type1
    [year] => 2014
)
</pre>
<pre>Array
(
    [id] => 1
    [name] => Part1
)
</pre>
<pre>Array
(
    [id] => 1
    [robots_id] => 1
    [parts_id] => 1
    [created_at] => 2014-01-01
)
</pre>

but when I add this line at the end of test function

$temp = $robot->robotsParts;
echo '<pre>' . print_r($temp->toArray(),true) . '</pre>';

It didn't print out anything. Can anyone tell me what I did wrong? Thank you so much.



360
Accepted
answer

I solved my problem. I need to specific the namespace when set haveMany relationship.