YII: HAS_MANY Relation Issue
YII newbie here.
I'm having issues with one-to-many relation.
We have following tables:
开发者_JS百科## users
uid
username
## notes
date
body
## users_notes
user_id
note_id
We got two models - Users:
'usersNotes' => array(self::HAS_MANY, 'UsersNotes', 'user_id'),
and Notes:
'usersNotes' => array(self::HAS_MANY, 'UsersNotes', 'note_id'),
Of course we also have a model for pivot table - users_notes:
return array(
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
'note' => array(self::BELONGS_TO, 'Notes', 'note_id'),
);
When trying to display notes in a foreach loop we're getting an error Invalid argument supplied for foreach()
in this line foreach($this->_pkAlias as $name=>$alias)
of /framework/db/ar/CActiveFinder.php(838)
file.
Do you have any ideas? Is there something we're missing/doing wrong?
If I understood your problem, you have a relation like "One user has multiple notes". In that case, you don't need a third table. Notes just needs one foreign key pointing to user_id in the Users table.
In case you need a many to many relation, just tell us.
精彩评论