我正在查看用户的组ID,然后将其与用户的组名进行比较。到目前为止我的代码是登录功能(仍在进行中):
$this->User->data = $this->data;
//find the user based on the data from the login form
$results = $this->User->findByemail ($this->data['User']['email']);
$this->User->id = $results['User']['id'];
$name = 'admin';
$admin['Group']['name'] = $this->User->Group->find('list', array('conditions' => array('Group.name' => $name)));
$str1 = $admin;
$str2 = 'admin';
if (strcmp($admin, 'admin'))
{
debug ('Yes, admin matches'); die();
}
到目前为止,我找到了群组ID,但我很难将名称与字符串' admin'进行比较。有什么想法吗?
答案 0 :(得分:1)
首先,您的$admin
似乎是一个数组,而strcmp()
将两个字符串作为参数。这种比较的结果可能不是你所期望的那样。
其次,find('list')
will return由$displayField
指定的字段,每个模型can be defined。您需要指定displayField或find()
中的字段 - 调用。
答案 1 :(得分:0)
首先您需要在用户模型中添加Group与User的关系,如:
public $belongsTo = array(
'Group'
);
并在用户表中添加group_id比在控制器中获取角色名称,如:
$results = $this->User->findByemail ($this->data['User']['email']);
$group = $results['Group']['name'];
之后你匹配字符串。