Sunday, 15 August 2010

Yii Relation MANY_MANY: How to get attributes from relation table -


I have three tables:

  create tab 'user' (`id`in (10) Unsigned NULL AUTO_INCREMENT, `name` varchar (160) Default zero, Primary key (` id`) Create table `event` (` id` integer (10) unsigned NULL AUTO_INCREMENT, `title` varchar (255) ) Utf8_unicode_ci NULL, primary key (`id`) create table match` user_join_event` (` user_id` integer (10) unsigned NULL, `event_id` integer (10) unsigned NULL,` created` datetime not NULL, primary Key (`user_id`,` Context_id`), give the constraint `user_join_event_ibfk_1` the foreign key (` user_id`) reference `user` (` id`), the barrier `user_join_event_ibfk_2` give the foreign key (` event_id`) reference `event` (` id`))   

is related to the user event by UserJoinEvent :

  'Events' = & gt; The array (auto :: MANY_MANY, 'event', 'user_join_event (user_id, event_id)'),   

I want to get the code created in user_join_event table

You can not get additional attributes on an included table with multiple totals But you can have has_many relation through this, do not forget to create a model named by UserJoinEvent user_join_event

Model User

  'userJoinEvent' => ; Array (auto :: HAS_MANY, 'UserJoinEvent', 'user_id'), 'event' = & gt; ,   

Model UserJoinEvent

  '; Array ('userJoinEvent' itself: 'EVENT_ID' via 'HAS_MANY,' event ', = & gt;), event' = & gt; The array (auto :: BELONGS_TO, 'event', 'event_id'),   

Cotroller (get the title and date of the event created with the user with P1 of example) $ Models = User :: Models () - & gt; FindByPk (1); Foreign currency ($ model-> user item $ item joint item) {echo $ item-> Event-> Title; Echo $ item-> Created; }

No comments:

Post a Comment