Hi,
I have the following 3 tables:
user-table:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
.........
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`username`),
UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
organisation-table:
CREATE TABLE IF NOT EXISTS `organisation` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`description` text,
`status` tinyint(4) NOT NULL,
`create_time` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
and the pivot table, organisation-roles:
CREATE TABLE IF NOT EXISTS `organisation_roles` (
`user_id` int(10) unsigned NOT NULL,
`organisation_id` int(10) unsigned NOT NULL,
`status` tinyint(4) NOT NULL,
`leader` tinyint(1) unsigned NOT NULL,
UNIQUE KEY `user_organisation` (`user_id`,`organisation_id`),
KEY `fk_organisation_roles_user_id` (`user_id`),
KEY `fk_organisation_roles_organisation_id` (`organisation_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now to my questions:
[b]I would like to view a organisation and display its leaders.
A user is a leader only if the leader flag/boolean is set to true in organisation_roles table.[/b]
The Organisation-MODEL has the following relations:
//'users' - relation is the generated one from giix
//'users' => array(self::MANY_MANY, 'User', 'organisation_roles(organisation_id, user_id)'),
'leaders' => array(self::MANY_MANY, 'User', 'organisation_roles(organisation_id, user_id)',
'condition'=>'leaders_leaders.leader = true', // leaders_leaders is the name for organisation_roles in the generated sql
),
In the view it is currently used like this:
...
<h2><?php echo GxHtml::encode($model->getRelationLabel('leaders')); ?></h2>
<?php
echo GxHtml::openTag('ul');
foreach($model->leaders as $relatedModel) {
echo GxHtml::openTag('li');
echo GxHtml::link(GxHtml::encode(GxHtml::valueEx($relatedModel)), array('user/view', 'id' => GxActiveRecord::extractPkValue($relatedModel, true)));
echo GxHtml::closeTag('li');
}
echo GxHtml::closeTag('ul');
...
I would like the somehow define a scope that i can use on this relation, that I dont need to add a new relation with condition for new roles. The new roles should then use the existing relation but by a differnt scope. Is that even the right approach for this problem (speaking of using scopes for different roles).
Should I use HAS_MANY with the through option? Does that replace MANY_MANY?