ar relation problem

i have three tables

  1. Profiles

    i have two columns userid,login_name

  2. Components

    i have three columns id,name,initialowner

    here initialowner points to Profiles->userid

    for every component only one user will present.

  3. Componentscc

    i have two columns user_id,component_id

    here userid points to Profiles->userid and component_id points to Components->id

    for every user multiple components and for every component multiple users

i have models and relations on every table like below




class Profiles extends CActiveRecord

{

    public function relations()

    {

         'initialowner' => array(self::HAS_MANY, 'Components', 'initialowner'),

         'componentcc' => array(self::MANY_MANY, 'Components', 'component_cc(user_id, component_id)'),

    }

}


class Components extends CActiveRecord

{

     public function relations()

    {

         'initialowner' => array(self::HAS_MANY, 'Profiles', 'initialowner'),

    }

}


class ComponentCc extends CActiveRecord

{

     public function relations()

    {

         

    }

}



i need all the users from ComponentCc table of component_id(pass dynamic value $_POST[‘component_id’]) then join to Profiles table to get their userid,login_id to first result and i need all the users(need their userid,login_name) who are not part of the component to second result.

Could some one help me on this.

As Componentscc have both tables PKs

so use it for relation as

Profile.php




 'components' => array(self::MANY_MANY, 'Components', 'component_cc( component_id,user_id)'),



Components.php




 'users' => array(self::HAS_MANY, 'Profiles', 'component_cc(user_id, component_id)'),



Finally my relations goes like this




class Profiles extends CActiveRecord

{

    public function relations()

    {

         'initialowner' => array(self::HAS_MANY, 'Components', 'initialowner'),

         'components' => array(self::MANY_MANY, 'Components', 'component_cc(user_id, component_id)'),

    }

}


class Components extends CActiveRecord

{

     public function relations()

    {

         'initialowner' => array(self::HAS_MANY, 'Profiles', 'initialowner'),

         'users' => array(self::MANY_MANY, 'Profiles', 'component_cc(user_id, component_id)'),

    }

}


class ComponentCc extends CActiveRecord

{

     public function relations()

    {

         

    }

}



Now i am trying to get the users from the componentscc table, i am using the following code to get the users, but its returning error saying components.component_id column not found.




Profiles::model()->with(array('components'=>array('condition'=>'components.component_id='.$_POST['component_id'])))->findAll();



any one help is much appreciated

Should be a MANY_MANY relationship




'users' => array(self::HAS_MANY, 'Profiles', 'component_cc(user_id, component_id)'),



/Tommy

My intention here is to get result the same as below query through my Model class




select p1.userid,p1.login_name from profiles p1 inner join Componentscc p2 on(p1.userid=p2.user_id) where p2.component_id=$_POST['component_id']



This query will return the users matching to the component_id of Componentscc table, and i want another result that users who are not matching to the componenet_id.

How can i do the same through my model classes.

Sorry, I didn’t read thoroughly. The solution I assumed was querying Components with users, condition id=$_POST[‘component_id’].

But first you should try thsi again replacing components_id with id




Profiles::model()->with(array('components'=>array('condition'=>'components.id='.$_POST['component_id'])))->findAll()



(not tested)

/Tommy

I managed my relations for my tables like below




class Profiles extends CActiveRecord

{

      public function relations()

      {

             return array(

                        'joinUserCc'=>array(self::HAS_MANY,'ComponentCc','user_id'),

                        'component'=>array(self::HAS_MANY,'Components','component_id','through'=>'joinUserCc'),

		);

      }

}


class Components extends CActiveRecord

{

      public function relations()

      {

             return array(

                        'jointCcUser'=>array(self::HAS_MANY,'ComponentCc','component_id'),

                        'users'=>array(self::HAS_MANY,'Profiles','user_id','through'=>'jointCcUser'),

		);

      }

}


class ComponentCc extends CActiveRecord

{

      public function relations()

      {

             return array(

                        'defaultccuser' => array(self::BELONGS_TO, 'Profiles', 'user_id'),

                        'componentid'=> array(self::BELONGS_TO, 'Components', 'component_id'),

		);

      }

}




I need all the users from profiles table based on component_cc table user_id column with a particular component_id for that i used my model class like below




$defaultCCusers=Profiles::model()->findAll(array(

                    'with'=>array(

                        'joinUserCc'=>array(

                            'select'=>false,

                            'condition'=>'component_id='.$_POST['component_id'],

                        ),

                    ),

                    'order'=>'login_name DESC',

                    'select'=>'userid,login_name'

                ));



This works perfectly fine, but i need another result, similar to above scenario(above scenario will return all matching users to component_id), now i need all users who are not matching to the component_id.

Some one help will be really appreciated.




$defaultCCusers=Profiles::model()->findAll(array(

                    'with'=>array(

                        'joinUserCc'=>array(

                            'select'=>false,

                            'condition'=>'component_id!='.$_POST['component_id'],

                        ),

                    ),

                    'order'=>'login_name DESC',

                    'select'=>'userid,login_name'

                ));