how do I delete rows in Yii?

49,745

Solution 1

A prettier solution is

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

Solution 2

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

Solution 3

Try this...

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

Solution 4

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

Share:
49,745
Ionut Flavius Pogacian
Author by

Ionut Flavius Pogacian

WILL.I.AM says it all

Updated on July 09, 2022

Comments

  • Ionut Flavius Pogacian
    Ionut Flavius Pogacian almost 2 years

    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);