CListWidget

Hi,

Is it possible to use models with CListWidget or does it have to be DataProvider?

James.

It requires a dataProvider object as parameter

Gustavo is correct. But, if you have to use an array of models then you can always use the CArrayDataProvider.

Ahh ok thanks.

I like using the models because I can do …

$table1->table2-table3 etc in a method chain etc.

However I also like CListViews pagination feature?

How do we handle this then? I cannot understand how Yii only provided CListViews as dataProvider and not models?

What do we do if we need pagination or sorting on a model and not dataProvider?

I don’t understand what you’re trying to achieve to be honest. If you pass in an CActiveDataProvider you have a model at the $data variable in the list view and can still make use of that model’s relationships.

Hi, I do not think you can still make use of method chaining and model relationships because the data provider runs a new query to the database table. It does not deal with the model or arrays of model objects.

Can OP use the ‘with’=>‘something’ property of the CDbCriteria for the chaining?

What do you mean you can’t use model relationships? I do this all the time!

Just like:




$dataProvider = new CArrayDataProvider(Model::model()->findAll());

foreach($dataProvider as $data){

  print_r($data); // Look at it and you'll see the relationships are set up.

}



Ok great thanks.

Ok it works, however it only works on relations between one table and another, but it does not work when there is more than one relations. example …

Model::model()->tableOne // works.

Model::model()->tableOne->tableTwo // does not work.

I suspect that the value of ->tableOne is an array and therefore you can’t call ->tableTwo on it?

No thats not it, tableOne returns an object.

Well actually it is abit weird. tableOne returns an array, however you can still call tableTwo on it.

$users = $user->findByPK(Yii::app()->user->id)->company->users; // this works.

$users = $user->findByPK(Yii::app()->user->id)->company; // and this works

$users is an array though.

Strange. Maybe the "->users" array item is different to the other array items in the array that it returns.

However it does not seem to affect anything when you stick the array into a foreach loop, when needing to display data from DB. So it is still strange.

Definitely the case!




$users = $user->findByPK(Yii::app()->user->id)->company->users; // $users is an array of user objects


$users = $user->findByPK(Yii::app()->user->id)->company; // $users is a company object



Try $user->findByPK(Yii::app()->user->id)->company->users[0]->company and it will work assuming assuming ->users is not empty.

Ahh yes of course, sorry Say_Ten.

Users is associated with a many to many to therefore is an array whereas company is a belongs to and therefore is just the company object.

To add to this though. I am still wondering why "company" works and not "users".

Jaymard wrote this.




$dataProvider = new CArrayDataProvider(Model::model()->findAll());



So the findAll is an array, so why does that work and not this.




$users = $user->findByPK(Yii::app()->user->id)->company->users;

$dataProvider = new CArrayDataProvider($users);



The company works as well.




$users = $user->findByPK(Yii::app()->user->id)->company;

$dataProvider = new CArrayDataProvider($users);



Which is strange