我怎么Yii中删除行?(how do I delete rows in Yii?)

2019-07-29 15:26发布

使用Yii,我想删除所有未从今天的行。

是我的解决办法好吗?

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

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

Answer 1:

更漂亮的解决方案

YourUserModel::model()->deleteAll("day !='" . date('Y-m-d') . "'");


Answer 2:

更好的用户PDO参数和命令你也必须调用execute

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

要么

UserLoginHash::model()->deleteAll(
    'day <> :date',
    array('date' => date('Y-m-d'))
);


Answer 3:

试试这个...

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


Answer 4:

您可以使用查询生成器

$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



文章来源: how do I delete rows in Yii?