Select CakePHP 3-除非特别选择,否则默认情况下在查询中排除字段

Select CakePHP 3-除非特别选择,否则默认情况下在查询中排除字段,select,cakephp,orm,cakephp-3.x,Select,Cakephp,Orm,Cakephp 3.x,标题几乎说明了一切。我有一些带有包含大量数据的字段的表。为了节省一些性能,我不希望在默认情况下选择这些选项。 强调新的违约行为,将问题与 例如: $cities = $this->Cities->find(); // A $city does not include the field `shape` (which is a huge polygon) 我查看了实体的accessible和hidden属性,但它们似乎不会影响SELECT语句 编辑:selectAllExcept查

标题几乎说明了一切。我有一些带有包含大量数据的字段的表。为了节省一些性能,我不希望在默认情况下选择这些选项。
强调新的违约行为,将问题与

例如:

$cities = $this->Cities->find();

// A $city does not include the field `shape` (which is a huge polygon)
我查看了实体的
accessible
hidden
属性,但它们似乎不会影响SELECT语句

编辑:
selectAllExcept
查询似乎很有用。我将其与
beforeFilter
事件组合如下:

public function beforeFind($event, $query, $options, $primary)
{
    $query->selectAllExcept($this, ['shape']);
}
这对于空查询很有效,
shape
现在被排除在外。但现在我无法控制其他可能要包含或不包含的字段:

$this->Cities->find()->select(['id','shape'])
也将选择其他字段,因为
selectAllExcept()
可以简单地覆盖表中的
find('all')
方法

例如,在UsersTable中:

public function findAll(Query $query, array $options)
{

    $query->selectAllExcept($this, ['password']);

    return $query;
}
然后在控制器中:

// select all except password
$users = $this->Users->find();
debug($users);

//我们尝试选择一些字段,包括密码,但成功了

$users=$this->users->find()->选择(['id','username','password'],true);//这回答了你的问题吗?部分原因是,我没有意识到
selectAllExcept
函数的存在。我的任务的剩余部分是默认情况下如何使用,请参阅示例;我想可能会出现这种情况,但在特定的用例中,我将无法使用可选的select语句。例如,如果在此表上使用其他位置的联接,并且由于查找前的
而包含
此表.id
,则任何
求和
计数
都可能会中断。您的代码现在看起来如何?啊,这也可以工作。你可能打错字了吗?最后两个控制器行之间有什么区别?最后一个选择示例有第二个参数,设置为overwrite true。
// select all except password
$users = $this->Users->find();
debug($users);
// we try to select some fields, without success
$users = $this->Users->find()->select(['id', 'username', 'password']); 
debug($users);
// we try to select some fields incl. password, with success
$users = $this->Users->find()->select(['id', 'username', 'password'], true); // <-- this overwrite select / selectAllExcept in custom finder
debug($users);