在 beforeFind 的 cakephp 2.x 中,我可以检查是否设置了某些条件
!empty($queryData['conditions'][$this->alias.'.field'])
或获取可通过 $queryData['fields 简单检索的字段列表']
。如何在 cakephp 3.x 中实现这一点?
在之前查找
public function beforeFind(Event $event, Query $query, $options, $primary)
{
}
选项
为空。我可以使用 $query
通过 $query->where(...)
添加条件,但是如何检查哪些字段被设置为要检索或哪些条件是已经申请了?
谢谢
最佳答案
取自the CakePHP 3.0 API documentation :
traverse( callable $visitor , array $parts [] )
Will iterate over every specified part. Traversing functions can aggregate results using variables in the closure or instance variables. This function is commonly used as a way for traversing all query parts that are going to be used for constructing a query.
The callback will receive 2 parameters, the first one is the value of the query part that is being iterated and the second the name of such part.
Example:
$query->select(['title'])->from('articles')->traverse(function ($value, $clause) {
if ($clause === 'select') {
var_dump($value);
}
}, ['select', 'from']);
所以只需调用 $query->traverse() 并提供回调闭包并在其中进行检查。另见 traverseExpressions() .
关于Cakephp 3 - 在 beforefind 中获取字段和条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35814310/