隐藏的字段仍从cakephp 3的数据库中列出 [英] Hidden fields are still listed from database in cakephp 3

查看:50
本文介绍了隐藏的字段仍从cakephp 3的数据库中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"get"和"find"方法从两个不同的点从数据库中获取记录.问题是,当我使用获取",第一"或最后"时,隐藏的字段不会显示(可以),但是当我使用查找"时,它们仍然存在.

I am getting the records from my database in two different points, using "get" and "find" methods. The problem is that when I am using "get", "first" or "last" the hidden fields aren't displayed (Its ok), but when I am using "find" they are still there.

<?php
//My Plugin in /plugins/Comunica/Files/src/Model/Entity/File.php
namespace Comunica\Files\Model\Entity;

use Cake\ORM\Entity;

class File extends Entity
{
  protected $_hidden = ['password'];
  protected $_virtual = ['protected'];

  protected function _getProtected(){
    return empty($this->_properties['protected']) ? false : true;
  }
}

调用方法:

<?php
    $this->Files->find()->toArray();

再次.仅调用一个记录(第一次,最后一次,调用)是正确的,尝试使用方法"find"是错误的.有人知道如何解决这个问题吗?

Again. It is right when calling just one record (first, last, call), It's just wrong when trying with method "find". Any one knows how to solve this?

推荐答案

我找到了此问题的答案.查找返回一个拥有每个结果的实体的对象,以便您可以使用表类中的"findAll"方法将其转换.

I have found an answer for this problem. The find returns an object that owns the entities of every result, so that you can convert them by using the "findAll" method inside the table's class.

<?php
//My Plugin in /plugins/Comunica/Files/src/Model/Entity/File.php
namespace Comunica\Files\Model\Entity;

use Cake\ORM\Entity;
use Cake\ORM\Query;//Include this class to manipulate the results

class File extends Entity
{
  protected $_hidden = ['password'];
  protected $_virtual = ['protected'];

  protected function _getProtected(){
    return empty($this->_properties['protected']) ? false : true;
  }

 //New formatation code
  public function findAll(Query $query, array $options)
  {
    return $query->formatResults(function ($results) {
      return $results->map(function($row) {
          $row['upload_date'] = $this->dateTimeConvert($row['upload_date']);
          return $row->toArray();
      });
    });
  }
}

这篇关于隐藏的字段仍从cakephp 3的数据库中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆