[英]In CakePhp, how can I retrieve the value of only one column from my database?
I am new to CakePHP but I have been using PHP for a while. 我是CakePHP的新手,但是我已经使用PHP已有一段时间了。 I am trying to create a helper that would provide the level of access of a user (ACL).
我试图创建一个提供用户访问级别(ACL)的帮助程序。
Here is my ACLHelper.php so far 到目前为止,这是我的ACLHelper.php
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\ORM\TableRegistry;
class ACLHelper extends Helper{
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id]);
return $acl->first();
}
}
I call this function in my view this way 我这样认为这个功能
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
And this is the output 这是输出
{ "is_items": "4" }
What I need is the function to return true or false if the value of the field equals or is higher then the value of $level
provided to the function. 我需要的是如果字段的值等于或
$level
提供给函数的$level
的值,则返回true或false的函数。 Now if I do this : 现在,如果我这样做:
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
it will return just the value. 它只会返回值。 My problem is that I do not want to specify the field twice.
我的问题是我不想两次指定该字段。
Thanks in advance for any help 预先感谢您的任何帮助
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
// Get the first ACL record right here
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id])->first();
// Compare the requested field against the provided level
return $acl->$acl_field >= $level;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.