how do I delete rows in Yii?

2019-04-21 11:41发布

Using Yii, I want to delete all the rows that are not from today.

Is my solution ok ?

$query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d',time()))."'";

Yii::app()->db->createCommand($query);

4条回答
Evening l夕情丶
2楼-- · 2019-04-21 11:54

A prettier solution is

YourUserModel::model()->deleteAll("day !='" . date('Y-m-d') . "'");
查看更多
地球回转人心会变
3楼-- · 2019-04-21 12:05

Better user PDO parameters and on command you also have to call execute

$query = "delete from `user_login_hash` where `day`<> :date";
$command = Yii::app()->db->createCommand($query);
$command->execute(array('date' => date('Y-m-d')));

or

UserLoginHash::model()->deleteAll(
    'day <> :date',
    array('date' => date('Y-m-d'))
);
查看更多
相关推荐>>
4楼-- · 2019-04-21 12:07

Try this...

 $query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d',time()))."'";
                        $query->queryAll($query);
查看更多
我只想做你的唯一
5楼-- · 2019-04-21 12:07

You may use query builder

$command = Yii::app()->db->createCommand()
    ->delete('user_login_hash', 'day !=' . date('Y-m-d'));

http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder#sec-15

查看更多
登录 后发表回答