Reputation: 358
I have created the phalcon models and relationship as per they provided the document but getting the error "Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$GroupsMembers in /var/www/html/uno/apps/webservice/controllers/UserController.php on line 245" while trying to fetch related records in many to many relationship.
I have 3 tables users, groups and group_members. group_members contain the relationship for groups and users.I want to get the all subscribed groups for a member therefore I have used below statements in UserController:
$user = Users::find('id = '.$user_id); $subscribed_groups = $user->groupsMembers; foreach($groupMembers as $groupMember){ echo $groupMember->groups->title; }
It gives the error mentioned above.
Please visit the link below to see my tables, models and controller.
Thanks.
Upvotes: 0
Views: 703
Reputation: 358
Thank you Calin Rada (http://forum.phalconphp.com/user/283/calinrada) for answer.
Correct answer is:
find() method is returning an instance of Phalcon\Mvc\Model\Resultset\Simple but findFirst() will return an instance of your object (User in your case).
Detail answer on: http://forum.phalconphp.com/discussion/5928/fetching-records-from-many-to-many-relationship-does-not-work-as
Upvotes: 1
Reputation: 1858
You use the alias GroupsMembers but then refer to it as $user->groupsMembers which is causing the undefined property notice.
Adjust your code to
$subscribed_groups = $user->GroupsMembers;
And it should work.
Upvotes: 0