gpt4 book ai didi

php - 如何从cakephp中的数据库表中获取id的值

转载 作者:行者123 更新时间:2023-11-29 11:36:51 24 4
gpt4 key购买 nike

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');
}
}

这是我到学校的第一个项目 cakephp 所以你们能帮助我吗? :)我对 php 很弱

<小时/>

感谢提示,但仍然不起作用。

我用这个

$id  =  $this->Auth->user('id');  // To get user id 
$this->set('id', $id);

在我的 AppController 中,在过滤器之前的 funciton 中。

我使用例如 if($id==1) .....但仍然不起作用。 var 转储显示 null。

我忘了说,但我有 CakePHP 3x 版本

最佳答案

要在 Controller 中设置用户任何信息,您可以使用

$this->Auth->user('fieldName');

AppController中的beforeFilter方法中只需设置您需要的用户信息,如下代码

$id  =  $this->Auth->user('id');  // To get user id 
$this->set('id', $id);

现在,在任何 View 中,您都可以应用类似条件

if($id==1) 
--do this--

如果你想在任何可以使用的地方获取用户 ID,在 cakephp 3.x 中不允许

AuthComponent::user('id')

详情可以查看doc

关于php - 如何从cakephp中的数据库表中获取id的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com