Self join with active record

I want users to be able to have a list of other users called "contacts," similar to a friends list.

I have a table of users called user.

I have a table with two columns, user_id and contact_id, called user_contact.

I would like to join the user table on itself using the user_contact table so that every user has a list of contacts.

So far, I have set up a relation in my user model that looks like this:




return array(

			'contacts'=>array(self::MANY_MANY, 'User',

				'user_contact(contact_id, user_id)'),

		);



In my controller I have this:




public function actionIndex()

	{

		$criteria = new CDbCriteria;

		

		$criteria->condition = "t.email = :email";

		

		$criteria->params = array(':email'=>Yii::app()->user->id);

		

		$criteria->with = array('contacts');

		

		$contactList=new CActiveDataProvider('User', array(

		        'pagination'=>array(

		            'pageSize'=>'5',

		        ),

		        'criteria'=>$criteria,

		    ));


		    $this->render('index',array(

		        'contactList'=>$contactList,

		    ));

	}



When I get to the view, I am dumping the contactList variable and it shows no contacts:




CActiveDataProvider#1

(

    [modelClass] => 'User'

    [keyAttribute] => null

    [CActiveDataProvider:_criteria] => CDbCriteria#2

    (

        [CDbCriteria:_paramCount] => 0

        [select] => '*'

        [distinct] => false

        [condition] => 't.email = :email'

        [params] => array

        (

            [:email] => 'demo@demo.com'

        )

        [limit] => -1

        [offset] => -1

        [order] => ''

        [group] => ''

        [join] => ''

        [having] => ''

        [with] => array

        (

            [0] => 'contacts'

        )

        [alias] => null

    )

    [CDataProvider:_id] => 'User'

    [CDataProvider:_data] => null

    [CDataProvider:_keys] => null

    [CDataProvider:_totalItemCount] => null

    [CDataProvider:_sort] => null

    [CDataProvider:_pagination] => CPagination#3

    (

        [pageVar] => 'User_page'

        [route] => ''

        [params] => null

        [CPagination:_pageSize] => '5'

        [CPagination:_itemCount] => 0

        [CPagination:_currentPage] => null

        [CComponent:_e] => null

        [CComponent:_m] => null

    )

    [CComponent:_e] => null

    [CComponent:_m] => null

)



No help? :(

Were you able to figure this out yet? I’m trying to accomplish the same thing, but am running into issues as well.

I am looking for solutions for exactly the same thing as this. Does anyone can please help?

Many thanks in advance!