Reputation: 121
I have three tables:
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(160) DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `event` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `user_join_event` (
`user_id` int(10) unsigned NOT NULL,
`event_id` int(10) unsigned NOT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`user_id`,`event_id`),
CONSTRAINT `user_join_event_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `user_join_event_ibfk_2` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`)
)
User
has relation with Event
by UserJoinEvent
:
'events' => array(self::MANY_MANY, 'Event', 'user_join_event(user_id, event_id)'),
I want to get created
time in user_join_event
table
Upvotes: 1
Views: 1457
Reputation: 56
You cannot get additional attributes on a joining table with many_many relation but You can do this through has_many relation. Don't forget to make a model named UserJoinEvent from table user_join_event
Model User
'userJoinEvent' => array(self::HAS_MANY, 'UserJoinEvent', 'user_id'),
'events' => array(self::HAS_MANY, 'Event', 'event_id', 'through'=>'userJoinEvent'),
Model UserJoinEvent
'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
Cotroller (example get the title of event and the date created from user with pk 1)
$model = User::model()->findByPk(1);
foreach ($model->userJoinEvent as $item) {
echo $item->event->title;
echo $item->created;
}
Upvotes: 4