从Yii1的会话公共功能搜索()中获取ID

时间:2018-10-12 07:48:12

标签: php yii yii1.x

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


    ));
}

我使用1之类的ID $criteria->addCondition('id=1'),而不仅仅是使用id=1显示记录,我该如何从用户季节登录中获取自动ID。

2 个答案:

答案 0 :(得分:0)

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;我认为它运行

答案 1 :(得分:0)

您的search()方法已经具有用于按用户ID过滤的代码:

$criteria->compare('id',$this->id);

您需要做的就是将模型的id属性设置为当前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模式,例如,您将无法在控制台中使用此方法。应当在控制器级别处理全局状态并将其传递给模型。