简体   繁体   English

将数组从控制器中的用户定义操作传递到Yii中的视图

[英]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. 在我的Yii应用程序中,我需要在控制器中执行联合查询并在视图中显示最终视图。 this is the mysql query I need to perform.I hav three tables namely,items,manufacturers,items_manufacturers 这是我需要执行的mysql查询。我有三个表,即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. 现在,您可以在控制器中更改actionJoint()方法。

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. 现在在_joinview页面上。

check print_r($data); 检查print_r($data); then retrieve data according to your need. 然后根据您的需要检索数据。

Now it is fine. 现在很好。

Hope it will help you. 希望对您有帮助。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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