[b]Hello,
I have a question regarding the MANY_MANY relationships inside Yii.
How do i get a result from $usergroups->i18n->groupname inside the following situation:[/b]
I have 4 database tables: (core_usergroups, core_usergroups_i18n, core_users, core_users_usergroups)
[sql]
// Table core_usergroups
CREATE TABLE IF NOT EXISTS core_usergroups (
id smallint(5) unsigned NOT NULL AUTO_INCREMENT,
module varchar(50) COLLATE utf8_unicode_ci NOT NULL,
name varchar(40) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id),
KEY name (name),
KEY module (module)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
// Table core_usergroups_i18n
CREATE TABLE IF NOT EXISTS core_usergroups_i18n (
usergroup varchar(40) COLLATE utf8_unicode_ci NOT NULL,
language char(2) COLLATE utf8_unicode_ci NOT NULL,
groupname varchar(40) COLLATE utf8_unicode_ci NOT NULL,
description varchar(400) COLLATE utf8_unicode_ci NOT NULL,
KEY usergroup (usergroup),
KEY language (language)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
// Table core_users
CREATE TABLE IF NOT EXISTS core_users (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
username varchar(15) COLLATE utf8_unicode_ci NOT NULL,
password varchar(40) COLLATE utf8_unicode_ci NOT NULL,
email varchar(200) COLLATE utf8_unicode_ci NOT NULL,
birthdate date DEFAULT NULL,
PRIMARY KEY (id),
KEY username (username),
KEY email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
// Table core_users_usergroups
CREATE TABLE IF NOT EXISTS core_users_usergroups (
user varchar(15) COLLATE utf8_unicode_ci NOT NULL,
usergroup varchar(40) COLLATE utf8_unicode_ci NOT NULL,
KEY user (user),
KEY usergroup (usergroup)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
[/sql]
[size=“1”]I don’t think i have to explain what the relationships are… but in short, the usergroups_i18n table has groupnames that can be displayed. So if the application is set to dutch, the language column will be nl and the groupname for group guest will be gast[/size]
Now the real question comes… I want to show the groupnames of each group that the user is member of, using the core_usergroups_i18n table on the users list page
As far as i know i have to do this inside the Users Model:
public function relations()
{
return array(
'usergroups' => array(self::MANY_MANY,'Usergroups','core_users_usergroups(user,usergroup)',
'with'=>'i18n'
),
);
}
And this inside the Usergroups Model
public function relations()
{
return array(
'i18n' => array(self::HAS_ONE, 'UsergroupsI18n', 'usergroup', 'condition'=>'language="en"'),
'users' => array(self::MANY_MANY, 'Users', 'core_users_usergroups(user,username)'),
);
}
But this throws me a [color="#FF0000"]"Invalid argument supplied for foreach()"[/color] when i try to display the i18n groupname inside a view:
<?php foreach($users as $user): ?>
Usergroups:
<?php foreach($user->usergroups as $key => $usergroup): ?>
<?php echo CHtml::encode($usergroup->i18n->groupname); ?>
<?php endforeach; ?>
<?php endforeach; ?>
