Here is the tables.
CREATE TABLE IF NOT EXISTS tt_users
(
id
int(11) NOT NULL AUTO_INCREMENT,
email
varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=21 ;
CREATE TABLE IF NOT EXISTS tt_groups
(
id
int(11) NOT NULL AUTO_INCREMENT,
title
varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
user_id
int(11) DEFAULT NULL,
PRIMARY KEY (id
),
KEY title
(title
),
KEY user_id
(user_id
),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS tt_group_members
(
id
int(11) NOT NULL AUTO_INCREMENT,
group_id
int(11) DEFAULT NULL,
user_id
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
Here are models:
-
Users model
public function getSource() { return 'tt_users'; } /**
-
Initialize relationship */ public function initialize(){
$this->hasManyToMany("id", "GroupsMembers", "user_id","group_id", "Groups", "id", array('alias' => 'GroupsMembers') );
}
-
-
Group model
public function getSource() { return 'uo_groups'; }
/**
-
Initialize relationship */ public function initialize(){
$this->hasManyToMany("id", "GroupsMembers", "group_id","user_id", "Users", "id", array('alias' => 'Users') );
}
-
-
Groups Members models, this table relate group and users.
public function getSource() { return 'uo_group_members'; }
/**
-
Initialize relationship */ public function initialize(){
$this->belongsTo("group_id", "Groups", "id", array('alias' => 'Group')); $this->belongsTo("user_id", "Users", "id", array('alias' => 'User'));
}
-
Below is the code from UserController where I need to list all subscribed groups for a user.
$user = Users::find('id = '.$user_id);
$subscribed_groups = $user->groupsMembers;
foreach($groupMembers as $groupMember){
echo $groupMember->groups->title;
}
I receive following error:
Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$GroupsMembers in /var/www/html/uno/apps/webservice/controllers/UserController.php on line 245
Right now I am using PHQL as alternate option but I expected to get results by relationship. Apperciate if someone help me to identify the issue.
Thanks.