Yii Framework Forum: Relational Query With Cdbcriteria And Cactivedataprovider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relational Query With Cdbcriteria And Cactivedataprovider Rate Topic: -----

#1 User is offline   Brando 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-January 13

Posted 27 May 2013 - 06:10 AM

Alright forgive me as I am still new to Yii. I am trying to use the yii generated search in the model for the admin page, however since each element has a uniqe number of objects "has_many" relationship I want the admin page to only show the accounts associated with that user. I have been having the biggest PITA trying to figure this out for some reason but everything I have done has not worked.

Code I am refering to:
public function search()
{
     // Warning: Please modify the following code to remove attributes that
     // should not be searched.

     $criteria=new CDbCriteria;
     $criteria->compare('id',$this->id);
     $criteria->compare('AccountId',$this->AccountId);
     $criteria->compare('APIUsername',$this->APIUsername,true);
     $criteria->compare('request_limit',$this->request_limit);
     $criteria->compare('send_limit',$this->send_limit);
     $criteria->compare('uploads_limit',$this->uploads_limit);
     $criteria->compare('downloads_limit',$this->downloads_limit);

     return new CActiveDataProvider($this, array(
          'criteria'=>$criteria,
     ));



What I have tried to do:

 $criteria=new CDbCriteria;
 $criteria->with=array('apiAdministrator');
 $criteria->together = true;



I also tried setting the constructor:

return new CActiveDataProvider(APIUsers::model()->with('apiAdministrator'), array(
     'criteria'=>$criteria,
));



but that also did not work.

I have seen alot of people explaining this when the criteria is just an array and not actually set with the CDbCriteria so I am lost on how to do this, I also setup the relationship correctly in each relations function. I have no issue setting up the relations under most things but I want to use this GII generated code because it does everything I need it to do just with the only modification is it being relational.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,997
  • Joined: 16-February 11
  • Location:Japan

Posted 27 May 2013 - 10:04 AM

Hi Brando,

Well, what do you want? Just to show the related models (apiAdministrator) with the main model (apiUser), or filter the main model by some field of the related model?

And what's the relation between the two? Is it "apiUser HAS_MANY apiAdministrators"?
0

#3 User is offline   Brando 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-January 13

Posted 27 May 2013 - 01:51 PM

So in the APIUser model I have this as a relationship:
'apiAdministrator'=>array(self::BELONGS_TO, 'User', 'AccountId'),


and in User model I have this:
'apiAccounts'=>array(self::HAS_MANY, 'ApiUsers', 'AccountId'),


I want a user to be able to go into the admin page of the API CRUD and be able to manage his api accounts and only his api accounts. This is the default GII generated crud at this point and I want to make it only point to tables related to ones user account. Is this relationship make sense? Is it setup correctly?

Thanks!
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,997
  • Joined: 16-February 11
  • Location:Japan

Posted 27 May 2013 - 06:01 PM

I see. So there are 2 models: User and ApiUsers.
ApiUsers BELONGS_TO User (apiAdministrator relationship) and
User HAS_MANY ApiUsers (apiAccounts relationship)


I guess you want to do is, first of all, making a custom version of "admin" page for ApiUsers model in which the results are filtered by "apiAdministrator"'s account ID. It's relatively simple.
public function actionAdminEx($accountId)
{
    $model = new ApiUsers('search');
    $model->unsetAttributes();
    if (isset($_GET['ApiUsers'])
        $model->attributes = $_GET['ApiUsers'];
    $model->AccountId = $accoutId;
    $this->render('adminEx', array('model'=>$model));
}

The difference from the original "admin" page is minimal. But by doing this, the grid in the page will show only the results that have a certain account ID.

BTW, the naming looks very confusing to me.
I would have named the models and the relations like the following:
ApiUser BELONGS_TO User (apiUser relationship) and
User HAS_MANY ApiUser (apiUsers relationship)

or
ApiAccount BELONGS_TO ApiAdministrator (apiAdministrator relationship) and
ApiAdministrator HAS_MANY ApiAccount (apiAccounts relationship)

It's just a naming, but for keeping things simple and consistent, I would follow the rules of:
1) Use a singular name for a model.
2) Use the same word for a model and a relation (if applicable).

Please take a look at this. It is the most highly rated wiki article. :)
http://www.yiiframew...-schema-design/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users