简体   繁体   English

在CakePhp中,如何从数据库中检索仅一列的值?

[英]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.

相关问题 在CakePhp中,如何从数据库中只检索一列? - In CakePhp, how can I retrieve only one column from my database? 无法提取数据库中数据的所有列,并且只有一列可以检索 - Not Fetch All column in the data in database and only one can retrieve 如何从表中检索列值到PHP变量? - How can I retrieve a column value from a table into a PHP variable? 无法从CakePHP的数据库中检索多个记录 - Can not retrieve multiple records from the database in CakePHP 如何使用单独的php文件从MySQL数据库检索一个值,并用从数据库中获取的值更改html控件? - How can I retrieve a one value from MySQL database using a seperate php file and change the html control with the value I got from the database? 如何根据列的值从数据库中获取列 - How can I get a column from database based on the value of the column 如何使用用户定义的其他列从数据库中检索单个列? CI / PHP - How can i retrieve a single column from a database using a different column defined by the user? CI/PHP 如何从多维数组中仅内插()一列? - How can I implode() only one column from a multidimensional array? 如何仅从数据库中获取 1 个值? - How can I fetch 1 value only from the database? cakephp:如何在我的下拉列表中显示默认值? - cakephp: how can i display a default value in my dropdownlist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM