1

当用户在 Whosloggedin 模型中按列用户名注销时,如何删除 yiiframework 中的记录?

表 - 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

我尝试过这样的事情

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

2 回答 2

2

传递给查找函数的参数是错误的。

在 Find 函数中,我们必须传递 $condition 和 $params 参数,默认情况下 $condition 为空且 $params 为空数组。

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

你的解决方案是:

$whosloggedin=Whosloggedin::model()->find('username=:username',array(':username'=>$users->username) ); 
$whosloggedin->delete();
于 2012-09-13T05:25:01.163 回答
0

签出beforeLogout()和类afterLogout()的方法CWebUser

您可以覆盖它们并在那里完成您的工作。不确定afterLogout()但肯定beforeLogout()仍然应该有对用户名的引用。

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

另外(在旁注中):

您可能还想在用户表中查询尚未注销但会话超时的用户。

关于查找用户:

用于findByAttributes()单行或findAllByAttributes()多于 1 行。

$user = Whosloggedin::model()->findByAttributes(array(
    'username' => Yii->app()->user->name,
));
于 2012-09-13T19:34:41.613 回答