我使用“get”和“find”方法从两个不同的点获取数据库中的记录。问题是,当我使用“get”,“first”或“last”时,隐藏的字段不显示(确定),但是当我使用“find”时,它们仍然存在。
using System.Activities;
using System.Activities.Statements;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Policy p = new Policy() { Premium = 100, Year = 2016 };
var inputPolicy = new InArgument<Policy>();
var theOutput = new OutArgument<object>();
Activity dynamicWorkflow = new DynamicActivity()
{
Properties =
{
new DynamicActivityProperty
{
Name="Policy",
Type=typeof(InArgument<Policy>),
Value=inputPolicy
}
},
Implementation = () => new Sequence()
{
Activities =
{
new Assign()
{
To = theOutput,
Value=new InArgument<string>() { Expression = "Policy.Premium * 1.05" }
}
}
}
};
WorkflowInvoker.Invoke(dynamicWorkflow);
}
}
public class Policy
{
public int Premium { get; set; }
public int Year { get; set; }
}
}
致电方法:
<?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;
}
}
再次。只调用一个记录(第一个,最后一个,调用)是正确的,尝试使用方法“find”时这是错误的。任何人都知道如何解决这个问题?
答案 0 :(得分:0)
我找到了这个问题的答案。 find返回一个拥有每个结果的实体的对象,以便您可以使用表类中的“findAll”方法转换它们。
<?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();
});
});
}
}
答案 1 :(得分:0)
我的主要目的是默认情况下排除隐藏字段,如果需要,可以显式获取包含隐藏字段的实体。
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;
}
要接收隐藏字段,您可以简单地添加 - >选择('密码')到您的查询,但为了使它更好,我添加了自定义查找器
ModelsTable.php
public function findSecrets(Query $query, array $options)
{
$tableSchema = $this->schema();
$fields = $tableSchema->columns();
return $query->select($fields);
}
现在,您可以构建一个这样的查询来接收包含隐藏字段的实体:
ModelsController.php
$secretModels = $this->Models->find()->find('secrets');
或您喜欢的任何查询,只需添加自定义查找器 注意:是不能使用 - &gt; get($ id)所以你必须使用 - &gt; findById($ id) - &gt; find('secrets') - &gt; first()
我很高兴知道您对此解决方案的看法或您将要改变的内容 - 随时可以推荐: - )