[英]how to get a value of id from database table in cakephp
CAKEPHP
我从表获取ID值有问题
我的表名为"users"
并且具有ID PK(int),username(txt) name(txt)
我努力了
$this->users->read('users.id)
获取用户ID,但不起作用。
我也试过
$this->request->session()->read('users.id')
我想让像
if($this->users->read('users.id'))==1
因为我想向具有其他ID的用户隐藏html代码。 我也尝试使用类似的方法..... read('users.username')=='admin'
但它们也无法正常工作
如您所见,尝试使用session()获取ID,但不尝试
我的登录功能是
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller'=>'Accounts']);
}
$this->Flash->error('blalalalala');
}
}
这是我第一次上学的蛋糕,所以你们可以帮我吗? :)我在php中比较弱
谢谢技巧,但仍然无法正常工作。
我用这个
$id = $this->Auth->user('id'); // To get user id
$this->set('id', $id);
在过滤器之前在我的AppController中起作用。
而且我使用例如if($id==1)
.....仍然不起作用。 var dump显示为空。
我忘了说,但是我有CakePHP 3x version
要在控制器中为用户设置任何信息,您可以使用
$this->Auth->user('fieldName');
在AppController
中的beforeFilter
方法中,只需设置所需的用户信息即可,如下面的代码所示
$id = $this->Auth->user('id'); // To get user id
$this->set('id', $id);
现在,在任何视图中,您都可以应用条件
if($id==1)
--do this--
如果您想在任何可以使用的地方获取用户ID,则在cakephp 3.x中不允许
AuthComponent::user('id')
有关详细信息,请参阅doc
您可以尝试以下方法:
$this->Auth->user('id'); // for controller action
要么
$this->Session->read('Auth.User.id'); // for controller and views
要么
AuthComponent::user('id') // anywhere
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.