[英]In CakePhp, how can I retrieve the value of only one column from my database?
我是CakePHP的新手,但是我已經使用PHP已有一段時間了。 我試圖創建一個提供用戶訪問級別(ACL)的幫助程序。
到目前為止,這是我的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();
}
}
我這樣認為這個功能
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
這是輸出
{ "is_items": "4" }
我需要的是如果字段的值等於或$level
提供給函數的$level
的值,則返回true或false的函數。 現在,如果我這樣做:
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
它只會返回值。 我的問題是我不想兩次指定該字段。
預先感謝您的任何幫助
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.