aintgel
aintgel

Reputation: 658

Deleting records in yiiframework model by column

How can I delete records in yiiframework when users logged-out in Whosloggedin model by column username?

table - whosloggedin

CREATE TABLE `erc_whosloggedin` (
  `id` int(11) DEFAULT NULL,
  `username` varchar(50) DEFAULT NULL,
  `complete_name` varchar(95) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `time` time DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I Have tried something like this

    $whosloggedin=Whosloggedin::model()->find($users->username); 
    $whosloggedin->delete();

Upvotes: 1

Views: 12626

Answers (2)

Gung Foo
Gung Foo

Reputation: 13558

check out beforeLogout() and afterLogout() methods of the CWebUser class.

You can override them and do your work there. Not sure about afterLogout() but surely in beforeLogout() there should still be a reference to the username.

http://www.yiiframework.com/doc/api/1.1/CWebUser#afterLogout-detail

Also (and on a sidenote):

you would probably also want to query the user table for users that haven't logged out but whos session timed out.

About finding the user:

use findByAttributes() for a single or findAllByAttributes() for more than 1 row.

$user = Whosloggedin::model()->findByAttributes(array(
    'username' => Yii->app()->user->name,
));

Upvotes: 0

Onkar Janwa
Onkar Janwa

Reputation: 3950

Parameters passed to find function is wrong.

In Find function we have to pass $condition & $params parameters, by default $condition is an empty & $params is an empty array.

find($condition='',$params=array()) 

You solution would be:

$whosloggedin=Whosloggedin::model()->find('username=:username',array(':username'=>$users->username) ); 
$whosloggedin->delete();

Upvotes: 2

Related Questions