[英]Get the ID from the session public function search () in Yii1
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->addCondition('id=1');
$criteria->compare('id',$this->id);
$criteria->compare('login',$this->login,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('random_pass',$this->random_pass,true);
$criteria->compare('default_number_of_devices',$this->default_number_of_devices);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array('defaultOrder'=>'name ASC',)
));
}
我用的ID 1
像$criteria->addCondition('id=1')
它不只是自動顯示與記錄id=1
,我怎樣才能從用戶登錄的季節自動ID。
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$sas= Yii::app()->user->id;
$criteria->addCondition("id=$sas");
$criteria->compare('id',$this->id);
$criteria->compare('login',$this->login,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('random_pass',$this->random_pass,true);
$criteria->compare('default_number_of_devices',$this->default_number_of_devices);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array('defaultOrder'=>'name ASC',)
));
}
像這樣的desc id $ sas = Yii :: app()-> user-> id; 我認為它運行
您的search()
方法已經有用於按用戶ID過濾的代碼:
$criteria->compare('id',$this->id);
您需要做的就是將模型的id
屬性設置為current的ID。 因此,您的控制器動作應如下所示:
$model = new MyModel('search');
if (isset($_POST['MyModel'])) {
$model->attributes = $_POST['MyModel'];
}
if (Yii::app()->user->isGuest) {
Yii::app()->user->loginRequired();
}
$model->id = Yii::app()->user->id;
$dataProvider = $model->search();
您不應該在模型中直接使用Yii::app()->user
這樣會破壞MVC模式,例如,您將無法在控制台中使用此方法。 全局狀態應在控制器級別處理並傳遞給模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.