我在CakePHP 3.2中工作。 有一个表 要检查它,我添加了 打印世界 strong>而不是 Hello strong>,因为没有从数据库中检索到数据,因为如果用户没有登录则 user_addresses code>,我正试图从中获取用户的所有记录 p>
public function myFun()
{
$ this-> loadModel('UserAddresses');
$ user_id = $ this-> Auth-> user('id');
$ userAddresses = $ this-> UserAddresses-> find('all ',[
'conditions'=> [
'user_id'=> $ user_id
]
]);
if(empty($ userAddresses)){
echo'Hello'; //仅用于测试
} else {
echo'World';
}
}
code> pre>
myFun code>到控制器的
beforeFilter code> p>
$ this-> Auth-> allow(['myFun']);
代码> pre>
$ user_id code>必须为空。 p>
div>
I'm working in CakePHP 3.2. There is a table user_addresses
from which I'm trying to fetch all records of an user
public function myFun()
{
$this->loadModel('UserAddresses');
$user_id = $this->Auth->user('id');
$userAddresses = $this->UserAddresses->find('all', [
'conditions' => [
'user_id' => $user_id
]
]);
if (empty($userAddresses)) {
echo 'Hello'; // for testing only
} else {
echo 'World';
}
}
To check it, I added myFun
to controller's beforeFilter
$this->Auth->allow(['myFun']);
and this prints World instead of Hello since there is no data retrieved from database because if user is not logged in then $user_id
must be empty.