简体   繁体   中英

passing an array from an user defined action in controller to a view in Yii

In my Yii application I need to perform joint queries in controller and display the final view in views. this is the mysql query I need to perform.I hav three tables namely,items,manufacturers,items_manufacturers

SELECT items.id,item_desc,manufacturers.id,manufacturers.name FROM items_manufacturers,items,manufacturers WHERE items_manufacturers.item_id=item.id AND items_manufacturers.manufacturer_id=manufacturers.id.

The relation between the models is

public function relations()
    {

            'item' => array(self::BELONGS_TO, 'Items', 'item_id'),
            'manufacturer' => array(self::BELONGS_TO, 'Manufacturers', 'manufacturer_id'),
            'itemsManufacturersLocations' => array(self::HAS_MANY, 'ItemsManufacturersLocations', 'items_manufacturer_id'),
        );

This is the Query I performed in the controller

public function actionJoint()
{

        $imf=ItemsManufacturers::model()->with('item','manufacturer')->findAll();
    $this->render('joint',array(
    'imf'=>$imf
    ));

}

This is the code I implemented in the view

<?php



$this->breadcrumbs=array(
    'joint',  
);
$this->menu=array(
    array('label'=>'Create ItemsManufacturers', 'url'=>array('create')),
    array('label'=>'Manage ItemsManufacturers', 'url'=>array('admin')),
);
?>
<h1> List Items and Manufacturers </h1>
<?php 
$this->widget('zii.widgets.CListView',array(
 'dataProvider'=>$imf,
'itemView'=>'_jointview',
)); ?>

My code for the render of partial of view

    <?php
   /* @var $this ItemsManufacturersController */
/* @var $data ItemsManufacturers */
    ?>

    <div class="view">



        <b><?php echo CHtml::encode($data->getAttributeLabel('item_desc')); ?>:</b>
        <?php echo CHtml::encode($data->item_desc); ?>
        <br />

        <b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
        <?php echo CHtml::encode($data->name); ?>
        <br />




</div>

But I am getting this error which I am unable to rectify.. Anybody help me with this.

Fatal error: Call to a member function getData() on a non-object in /var/www/yii_framework/framework/zii/widgets/CBaseListView.php on line 107

Any body help me how should I proceed since I am a newbie

I think you are doing mistake on view listing code.

<?php
$this->breadcrumbs=array(
    'joint',  
);
$this->menu=array(
    array('label'=>'Create ItemsManufacturers', 'url'=>array('create')),
    array('label'=>'Manage ItemsManufacturers', 'url'=>array('admin')),
);
?>
<h1> List Items and Manufacturers </h1>
<?php 
$this->widget('zii.widgets.CListView',array(

'dataProvider'=>$dataProvider,,
'itemView'=>'_jointview',
)); ?>

Now you change your actionJoint() method in your controller.

public function actionJoint()
{

 $imf=ItemsManufacturers::model()->with('item','manufacturer')->findAll();

 $dataProvider=new CArrayDataProvider($imf, array(
 'id'=>'ItemsManufacturers',
 'sort'=>array(
    'attributes'=>array(
         'item_desc', 'name'
    ),
 ),
 'pagination'=>array(
    'pageSize'=>10,
 ),));

 $this->render('joint',array('dataProvider'=>$dataProvider));       

}

Now on _joinview page.

check print_r($data); then retrieve data according to your need.

Now it is fine.

Hope it will help you.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM