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

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.



555
Accepted
answer

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