Many To Many Relations User Module

Good day,

I am building many to many relationship across three tables:

User(user_id) ---- Registrations(user_id, quiz_id) ------ Quizzes(quiz_id)

User class is in user module but the rest two are under main model folder

So under User class:

I’ve put<br>

public function relations()


$relations = Yii::app()->getModule(‘user’)->relations;

if (!isset($relations[‘profile’]))

 &#036;relations['profile'] = array(self::HAS_ONE, 'Profile', 'user_id');

if (!isset($relations[‘quizzes’]))

 &#036;relations['quizzes'] = array(self::MANY_MANY, 'Quizzes', 'registrations(user_id, quiz_id)');

 return &#036;relations;


In Quizzes, I’ve put: <br>

public function relations(){



return array(

 'user' =&gt; array(self::MANY_MANY, 'User', 'registrations(quiz_id,user_id)'),



‘registrations’ is the table name in Registrations model:

public function tableName()


return ‘{{registrations}}’;


Seems I had everything done but still doesn’t work, can any one help with this please, appreciated ^^.

That is very confusing code :blink:

  1. Where files are located is a problem that you generally solve in the configuration (config/main.php) and has nothing to do with the relations between the models.

  2. You do not need to check whether or not a relation is already set…

The relations() function should just return an array that describes how your models are related, nothing more, nothing less.