Hidden fields are still listed from database in cakephp 3

不羁岁月 提交于 2019-12-25 08:49:21

问题


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;
  }
}

The Call Method:

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

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?


回答1:


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();
      });
    });
  }
}



回答2:


I solved it like this:

My main aim was to exclude hidden fields by default and have a way to explicitly get Entitys including hidden fields if I need them.

ModelsTable.php

public function beforeFind(Event $event, Query $query){

    //ATTENTION: if password field is excluded we have to bypass for Auth-Component to work
    if(array_key_exists('password',$_REQUEST)){
        return $event;
    }

    $protected = $this->newEntity()->hidden;

    $tableSchema = $this->schema();

    $fields = $tableSchema->columns();
    foreach($fields as $key => $name){
        if(in_array($name,$protected)){
            unset($fields[$key]);
        }
    }
    $query->select($fields);

    return $event;
}

Model.php

protected $_hidden = [
    'password',
    'otherSecret'
];

protected function _getHidden(){
    return $this->_hidden;
}

To receive hidden fields you can simple add ->select('password') to your query, but to make it more nice I added a custom finder

ModelsTable.php

public function findSecrets(Query $query, array $options)
{
    $tableSchema = $this->schema();
    $fields = $tableSchema->columns();
    return $query->select($fields);
}

Now you can build a query like this to receive Entity including hidden fields:

ModelsController.php

$secretModels = $this->Models->find()->find('secrets');

or whatever query you loke, simply add the custom finder NOTE: is does not work with ->get($id) so you have to use ->findById($id)->find('secrets')->first()

I'm happy to know what you think about this solution or what you would change - feel free to commend :-)



来源:https://stackoverflow.com/questions/40226293/hidden-fields-are-still-listed-from-database-in-cakephp-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!