tuschkan
(O Turansky)
July 15, 2011, 12:42am
1
There is a sale, which has many to many relation with items to be sold under this very sale (items can also be included to other sales). I want to list all the items for a selected sale with CListView. But I just cant figure out how to make it.
if(!$model = InshopSale::model()->findByPk($id)) {
throw new CHttpException(404, 'Nothing found.');
}
and what do I do next?
Thanks!!!
softark
(Softark)
July 15, 2011, 4:31am
2
If the relation is defined correctly, then you can access the available items under the specific sale by $model->relation_name like this:
if(!$model = InshopSale::model()->findByPk($id)) {
throw new CHttpException(404, 'Nothing found.');
}
echo "Item count = " . count($model->items) . "<br />\n";
foreach( $model->items as $item )
echo "Item name = " . $item->name . "<br />\n";
tuschkan
(O Turansky)
July 15, 2011, 6:26am
3
softark:
If the relation is defined correctly, then you can access the available items under the specific sale by $model->relation_name like this:
if(!$model = InshopSale::model()->findByPk($id)) {
throw new CHttpException(404, 'Nothing found.');
}
echo "Item count = " . count($model->items) . "<br />\n";
foreach( $model->items as $item )
echo "Item name = " . $item->name . "<br />\n";
I know that. but what I want it to use CListView and I need to pass data into it in a form of a dataprovider.
softark
(Softark)
July 15, 2011, 7:55am
4
What is the data item in the CListView?
If it is an InshopSale, "$data->items" in the "_view.php" can be used for Items?
tuschkan
(O Turansky)
July 15, 2011, 9:53am
5
softark:
What is the data item in the CListView?
If it is an InshopSale, "$data->items" in the "_view.php" can be used for Items?
InshopSale has storeItems(M:M). I need to load on the page the following data:
date (from - till) and a banner of the given sale.
all store items, that are included into this sale.
it is exactly “$data->items” in the “_view.php” that I want to use to list the store items, but I don’t know how to pass them to CListView.
netmek
(Zu)
July 15, 2011, 3:32pm
6
You can use CListView::viewData property to pass additional vars to your itemView. Have a look at the API doc - viewData
softark
(Softark)
July 16, 2011, 2:29am
7
Um, I think there’s no need to use viewData in this case.
A CListView is very similar to a CGridView, both of them use a data provider to work. And with both of them, you don’t have to retrieve each item by yourself.
I mean, you don’t have to write:
$model = InshopSale::model()->findByPk($id);
And in "_view.php", the data provider will give you the model instance of each list item by the name of "$data".
http://www.yiiframework.com/doc/api/1.1/CListView/
tuschkan
(O Turansky)
July 16, 2011, 6:05am
8
I’ve solved it the following way:
if(!$model = InshopSale::model()->findByPk($id)) {
throw new CHttpException(404, 'Nothing found.');
}
$dataProvider = new CArrayDataProvider($model->inshopItems, array(
'pagination' => array (
'pageSize' => Yii::app()->user->getState('pageSize', 20),
),
));
$model->inshopItems returns relates items and CArrayDataProvider implements a data provider based on a raw data array.