在我的Yii应用程序中,我需要在控制器中执行联合查询,并在视图中显示最终视图。这是我需要执行的mysql查询。我有三张表,即,项目,制造商,项目_制造商
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页面(_Y)。
检查print_r($data);
,然后根据需要检索数据。
现在一切都好了。
希望它能帮助你。
感谢