在Yii中添加相等和不相等的条件

时间:2014-08-27 05:32:20

标签: php activerecord yii equals-operator

$id = Yii::app()->user->getState("logId");
$name = Yii::app()->user->getState("username"); 

$dataProvider=Disnotification::model->findAllByAttributes(array('logID' => $id,'username'!=$name));

如何查找等于$id但不等于$name的记录?

3 个答案:

答案 0 :(得分:3)

$criteria = new CDbCriteria();
$criteria->compare('logID' , $id);
$criteria->addCondition('username != :username');
$criteria->params[ ':username' ] = $name;

$dataProvider = Disnotification::model()->findAll( $criteria );

答案 1 :(得分:3)

$criteria=new CDbCriteria;
$criteria->condition = "username != '$name' AND logID = :id";
$criteria->params = array (
    ':id' => $id,
);
$dataProvider = Disnotification::model()->findAll( $criteria );

答案 2 :(得分:0)

$dataProvider = Disnotification::model() -> findAllByAttributes(
        array(
            'logId'=>$id,
        ),
        new CDbCriteria(
            array(
                'order'=>'x ASC, y DESC',
                'condition'=>'username<>'.$username,
            ))
    );