Yii 中的语句有什么问题? [英] What is wrong with the statement,in Yii?
问题描述
我的控制器中有以下代码,
I have a folowing code in my controller ,
public function actionViewJob() {
$user_id = Yii::app()->session['user_id'];
/* For User Authentication */
if (Yii::app()->user->getId() === null)
$this->redirect(array('site/login'));
/* For User Authentication */
$model=ViewJob::model()->findAll(array('user_id'=>Yii::app()->user->id));
$params = array('model' => $model,
);
$this->render('viewjob', $params);
我收到错误/* 未定义属性CDbCriteria.user_id".*/但是当我使用 findByAttrinute 它的工作 f9 时,但我没有得到结果,即它没有过滤数据,请帮忙.
I am getting error /* Property "CDbCriteria.user_id" is not defined. */ but when I am using findByAttrinute its working f9,but I not getting the result,i.e its not filtering data,pls help.
查看部分:
// not posting full code //
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' =>$model->search(),
'columns' => array(
array(
'name' => 'title',
'type' => 'raw',
'value' => 'CHtml::encode($data->title)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
// 'filter'=>'false' /* for hiding filter boxes */
),?>
推荐答案
错误的方法.您应该使用 CActiveRecord::findAllByAttributes()
代替
Wrong method. You should use CActiveRecord::findAllByAttributes()
instead
$model=ViewJob::model()->findAllByAttributes(array('user_id'=>Yii::app()->user->id));
或者如果你仍然想使用 findAll
你应该把属性作为条件传入:
Or if you still want to use findAll
you should pass the attribute in as a condition:
$model=ViewJob::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
现在来看:
dataProvider
需要一个 CDataProvider
的实例.CActiveRecord::search()
返回一个这样的实例:CActiveDataProvider
.但是,$model
是一个数组,而不是 CActiveRecord
的实例.您在这里有两个选择:
dataProvider
expects an instance of CDataProvider
. CActiveRecord::search()
returns one such instance: CActiveDataProvider
. However, $model
is an array not an instance of CActiveRecord
. You have two choices here:
a) 您可以编辑控制器以将 $model
用作 ViewJob
实例而不是 ViewJob
实例的数组:>
a) You can to edit your controller to use $model
as a ViewJob
instance and not an array of ViewJob
instances:
public function actionViewJob() {
$user_id = Yii::app()->session['user_id'];
/* For User Authentication */
if (Yii::app()->user->getId() === null)
$this->redirect(array('site/login'));
/* For User Authentication */
$model= new ViewJob;
$model->user_id = $user_id;
$params = array('model' => $model,
);
...
b) 将视图中的 $model->search()
替换为
b) Replace the $model->search()
in the view with
'dataProvider' => new CArrayDataProvider($model)
选择 (b) 更容易,但它打破了约定,例如 $model
是一个数组而不是一个对象.此外,任何过滤器/搜索功能都必须手动添加,而不是使用 CActiveRecord::search()
提供的默认功能.
Choice (b) is easier but it breaks conventions e.g $model
is an array not an object. Also any filters/search functionality has to be added manually as opposed to using the default functionality provided by CActiveRecord::search()
.
这篇关于Yii 中的语句有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!