Difference between #6 and #7 of
Accessing data in a join table with the related models

Revision #7 has been created by erikj on Jul 15, 2012, 12:39:42 PM with the memo:

Changing "moviesJoin" to "watched" since "moviesJoin" is not defined anywhere, making the script not work.
« previous (#6) next (#8) »


Title unchanged

Accessing data in a join table with the related models

Category unchanged


Yii version unchanged

Tags unchanged

relations, active record, many_many, join table

Content changed

public function relations() {
return array(
'watched' => array(self::HAS_MANY, 'ViewerWatchedMovie', 'viewer_id'),
'movies' => array(self::HAS_MANY, 'Movie', 'movie_id',
'through' => '
To make it eager: `Viewer::model()->with('watched', 'movies')->findByPk($id)`.

I have it on [good authority](http://www.yiiframework.com/forum/index.php?/topic/8581-selecting-join-table-with-mant-to-many/page__st__40__p__123710__hl__creocoder#entry123710 "forum link") that the coherence of indexes of the two arrays `$viewer->
moviesJoinwatched` and `$viewer->movies` is assured. But this fact (feature?) is not documented so it makes me nervous. That's why I prefer a variation.

Add a relation to the `ViewerWatchedMovie` model:

class ViewerWatchedMovie extends CActiveRecord {
16 1
Viewed: 120 443 times
Version: 1.1
Category: How-tos
Written by: fsb
Last updated by: krowe
Created on: Dec 5, 2011
Last updated: 5 years ago
Update Article


View all history