php - Merging an array with DataProvider data items -
i have 2 tables in yii application; category , item.
category - id - name item - id - category_id - name
what list of items in category table , subsequently count number of items of each category in item table.
i'm using cactivedataprovider retrieve list of items in the category table , subsequently use for-loop count number of items in each category. however, can't seem figure out how merge array dataprovider data items.
here code far.
public function actionindex() { $dataprovider = new cactivedataprovider('category', array( 'criteria' => array( 'select' => 'id, name, slug', 'order' => 'name asc', ), )); $dataprovider->setpagination(false); $count = array(); foreach($dataprovider->getdata() $categorydata) { $count[] = item::model()->count("category_id = :categoryid", array("categoryid" => $categorydata->id)); } $this->render('index', array( 'dataprovider' => $dataprovider, 'count' => $count, )); }
how merge $count array existing dataprovider data items may use clistview? there more better solution this?
you shouldn't need seperate count array if relationships setup correctly. index
view looks this:
<h1>messages</h1> <?php $this->widget('zii.widgets.clistview', array( 'dataprovider'=>$dataprovider, 'itemview'=>'_view', )); ?>
then _view
should this:
<div class="view"> <b><?php echo chtml::encode($data->getattributelabel('id')); ?>:</b> <?php echo chtml::link(chtml::encode($data->id), array('view', 'id'=>$data->id)); ?> <br /> <b><?php echo chtml::encode($data->getattributelabel('name')); ?>:</b> <?php echo chtml::encode($data->name); ?> <br /> <b>items:</b> <?php echo count($data->items); ?> <br /> </div>
this assuming category
model has this:
public function relations() { return array( 'items' => array(self::has_many, 'item', 'category_id'), ); }
Comments
Post a Comment