Hah, with a help RuudBurger i managed to get it working, for people who want to know how it’s done now i’ll show you my current code:
you can name the tables how you want and the classes and all but this isn’t optimized yet.
SQL:
--
-- Table structure for table `rel_users_usergroups`
--
CREATE TABLE IF NOT EXISTS `rel_users_usergroups` (
`ID` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
-- --------------------------------------------------------
--
-- Table structure for table `Users`
--
CREATE TABLE IF NOT EXISTS `Users` (
`user_id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(200) NOT NULL,
`roleid` int(11) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
-- --------------------------------------------------------
--
-- Table structure for table `Users_Groups`
--
CREATE TABLE IF NOT EXISTS `Users_Groups` (
`group_id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`short` varchar(50) NOT NULL COMMENT 'capital reference name',
PRIMARY KEY (`group_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
At this point you can fill in a user, a group and a relation into your database to avoid errors from here on
yiic shell:
model Users
model Users_Groups
model rel_users_usergroups
crud Users
crud Users_Groups
In models/Users.php
added relation:
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'groups' => array(self::MANY_MANY, 'Users_Groups', 'rel_users_usergroups(user_id, group_id)')
);
}
in contollers/UsersController.php
[EDIT: Added RuudBurger’s suggestions]
new action:
/**
* Shows a particular model in detail.
*/
public function actionDetail()
{
$user = Users::model()->with('groups')->findByPk($_GET['id']);
$this->render('detail', array('user'=> $user));
}
added views/users/detail.php
[EDIT: Added RuudBurger’s suggestions]
<h2>View User Details for <?php echo $model->username; ?></h2>
<div class="actionBar">
[<?php echo CHtml::link('Users List',array('list')); ?>]
[<?php echo CHtml::link('New Users',array('create')); ?>]
[<?php echo CHtml::link('Update Users',array('update','id'=>$user->user_id)); ?>]
[<?php echo CHtml::linkButton('Delete Users',array('submit'=>array('delete','id'=>$user->user_id),'confirm'=>'Are you sure?')); ?>
]
[<?php echo CHtml::link('Manage Users',array('admin')); ?>]
</div>
<table class="dataGrid">
<tr>
<th class="label"><?php echo CHtml::encode($user->getAttributeLabel('username')); ?>
</th>
<td><?php echo CHtml::encode($user->username); ?>
</td>
</tr>
<tr>
<th class="label"><?php echo CHtml::encode($user->getAttributeLabel('password')); ?>
</th>
<td><?php echo CHtml::encode($user->password); ?>
</td>
</tr>
<tr>
<th class="label"><?php echo CHtml::encode($user->getAttributeLabel('email')); ?>
</th>
<td><?php echo CHtml::encode($user->email); ?>
</td>
</tr>
<tr>
<th class="label">Groups:
</th>
<td>
<?php foreach($user->groups as $group)
{
echo CHtml::link($group->name,array('users_Groups/detail','id'=>$group->group_id));
} ?>
</td>
</tr>
</table>
In models/Users_Groups.php
added relation:
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'users' => array(self::MANY_MANY, 'Users', 'rel_users_usergroups(group_id, user_id)')
);
}
in contollers/Users_GroupsController.php
[EDIT: Added RuudBurger’s suggestions]
new action:
/**
* Shows a particular model in detail.
*/
public function actionDetail()
{
$group= Users_Groups::model()->with('users')->findByPk($_GET['id']);
$this->render('detail', array('group'=> $group));
}
added views/users_Groups/detail.php
[EDIT: Added RuudBurger’s suggestions]
<h2>View Group Details for <?php echo $group->name; ?></h2>
<div class="actionBar">
[<?php echo CHtml::link('Groups List',array('list')); ?>]
[<?php echo CHtml::link('New Group',array('create')); ?>]
[<?php echo CHtml::link('Update Group',array('update','id'=>$group->group_id)); ?>]
[<?php echo CHtml::linkButton('Delete Groups',array('submit'=>array('delete','id'=>$group->group_id),'confirm'=>'Are you sure?')); ?>
]
[<?php echo CHtml::link('Manage Groups',array('admin')); ?>]
</div>
<table class="dataGrid">
<tr>
<th class="label"><?php echo CHtml::encode($group->getAttributeLabel('name')); ?>
</th>
<td><?php echo CHtml::encode($group->name); ?>
</td>
</tr>
<tr>
<th class="label"><?php echo CHtml::encode($group->getAttributeLabel('short')); ?>
</th>
<td><?php echo CHtml::encode($group->short); ?>
</td>
</tr>
<tr>
<th class="label">Users:
</th>
<td>
<?php foreach($group->users as $user)
{
echo CHtml::link($user->username,array('users/detail','id'=>$user->user_id));
} ?>
</td>
</tr>
</table>
[b]One final note:
You might want to add the page details to your rules, or you might end up in not seeing your page at all due to unauthorised access.[/b]