Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4811

how do I delete rows in Yii?

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

Upvotes: 11

Views: 50939

Answers (4)

Artem
Artem

Reputation: 19

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

Upvotes: 2

adamors
adamors

Reputation: 2656

A prettier solution is

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

Upvotes: 40

Imre L
Imre L

Reputation: 6249

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

Upvotes: 12

Owais Iqbal
Owais Iqbal

Reputation: 549

Try this...

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

Upvotes: 2

Related Questions