从Yii1的session公共函数search()中获取ID

fnx2tebb  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(149)
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。

bvk5enib

bvk5enib1#

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

    ));
}

这是一个很好的例子。我想它跑了

laik7k3q

laik7k3q2#

您的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模式,并且您将无法在控制台中使用此方法例如,全局状态应该在控制器级别处理并传递给模型

相关问题