help selecting model with another model

Hello experts, I need some help figuaring out how to properly select a model->with(‘another’) model…


$user = User::model()->with('auth')->findByAttributes(array('user_email' => 'email@email.com', 'confirmed' => 1));

        print_r($user->auth);

but $user->auth returns an empty array. Not sure what I’m doing wrong…

My User relations:


public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

            'auth' => array(self::MANY_MANY, 'UserAuth', '{{user_auth}}(user_id, auth_id)'),

        );

	}

My UserAuth relations:




public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

            'user' => array(self::BELONGS_TO, 'User', 'user_id'),

		);

	}

The print_r returns: Array ( )

Thank you!

I guess the relation of User against UserAuth is "User" HAS_MANY "UserAuth"s in your case.


public function relations()

	{

		return array(

                        'auth' => array(self::HAS_MANY, 'UserAuth', 'user_id'),

                );

	}

Or, if you want the relation of MANY_MANY between "User" and "Auth", i.e., a "User" has MANY "Auth"s and an "Auth" has MANY "User"s, then you will need 3 tables … 1)User, 2)Auth, and 3)User_Auth.




User

  - id

  - username

  - user_email

  - confirmed

  - ....


Auth

  - id

  - name

  - ....


User_Auth

  - user_id

  - auth_id



Ahh yes! I had assumed I needed to declare some relations in UserAuth too even though the gii model generator didn’t put anything in there. I changed the relationships and the array is coming back perfectly. Thank you!!