以下Criteria
可以正常使用:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$dp1 = new CActiveDataProvider('post', array(
'criteria' => $criteria
));
但是直接在模型上指定Criteria
不会 - 它没有效果:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$dp1 = new CActiveDataProvider(Mdlfood::model()->find($criteria),array(),));
以下也不起作用:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$model1 = new Mdlfood;
$model1->findAll($criteria);
$dp1 = new CActiveDataProvider($model1,array(),));
任何人都可以解释为什么我不能直接声明这个配置吗?
class Mdlfood extends CActiveRecord{
public function tableName()
{
return 'tblfood';
}
public function rules()
{
return array(
array('name, Url_picture, Price, Aboute, Id_foodType, Id_menu', 'required'),
array('name', 'length', 'max'=>100),
array('Url_picture, Aboute', 'length', 'max'=>2048),
array('Price, Id_foodType, Id_menu', 'length', 'max'=>20),
array('Id, name, Url_picture, Price, Aboute, Id_foodType, Id_menu', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'idMenu' => array(self::BELONGS_TO, 'Tblrestmenu', 'Id_menu'),
'idFoodType' => array(self::BELONGS_TO, 'Tblfoodtype', 'Id_foodType'),
);
}
public function attributeLabels()
{
return array(
'Id' => 'ID',
'name' => 'Name',
'Url_picture' => 'Url Picture',
'Price' => 'Price',
'Aboute' => 'Aboute',
'Id_foodType' => 'Id Food Type',
'Id_menu' => 'Id Menu',
);
}
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('Id',$this->Id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('Url_picture',$this->Url_picture,true);
$criteria->compare('Price',$this->Price,true);
$criteria->compare('Aboute',$this->Aboute,true);
$criteria->compare('Id_foodType',$this->Id_foodType,true);
$criteria->compare('Id_menu',$this->Id_menu,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
但是,我认为这与Model无关,因为此Criteria在ActiveDataProvider中正常工作。
答案 0 :(得分:0)
请尝试这样,
额外的右括号,find() - >格式不正确
$criteria=new CDbCriteria(array(
'condition'=>'Id_menu = 1 ',
'select'=>'name',
'limit'=>5,
));
$dp1 = new CActiveDataProvider(Mdlfood::model()->find($criteria),array()); // extra closing brackets
第二种方式,
$criteria=new CDbCriteria(array(
'condition'=>'Id_menu = 1 ',
'select'=>'name',
'limit'=>5,
));
$model1 = new Mdlfood;
$model1->findAll($criteria);
$dp1 = new CActiveDataProvider($model1,array());