PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Hidden fields are still listed from database in cakephp 3

 March 09, 2022     cakephp-3.0, php     No comments   

Issue

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?


Solution

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


Answered By - Shayllis Alves de Sousa
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing