繁体   English   中英

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

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

在我的Yii应用程序中,我需要在控制器中执行联合查询并在视图中显示最终视图。 这是我需要执行的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.

模型之间的关系是

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'),
        );

这是我在控制器中执行的查询

public function actionJoint()
{

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

}

这是我在视图中实现的代码

<?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',
)); ?>

我的部分视图渲染代码

    <?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>

但是我遇到了无法纠正的错误。任何人都可以帮助我。

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

因为我是新手,任何人都可以帮助我该如何进行

我认为您在查看列表代码时出错。

<?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',
)); ?>

现在,您可以在控制器中更改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));       

}

现在在_joinview页面上。

检查print_r($data); 然后根据您的需要检索数据。

现在很好。

希望对您有帮助。

谢谢

暂无
暂无

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

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