Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I delete rows in Yii?

Tags:

delete-row

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);
like image 939
Ionut Flavius Pogacian Avatar asked May 29 '12 13:05

Ionut Flavius Pogacian


4 Answers

A prettier solution is

YourUserModel::model()->deleteAll("day !='" . date('Y-m-d') . "'");
like image 88
adamors Avatar answered Nov 13 '22 08:11

adamors


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'))
);
like image 12
Imre L Avatar answered Nov 13 '22 10:11

Imre L


Try this...

 $query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d',time()))."'";
                        $query->queryAll($query);
like image 2
Owais Iqbal Avatar answered Nov 13 '22 08:11

Owais Iqbal


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

like image 2
Artem Avatar answered Nov 13 '22 08:11

Artem