Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 334 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cakephp 3 - get fields and conditions in beforefind

#1
In cakephp 2.x in beforeFind I could check if some condition is set by
`!empty($queryData['conditions'][$this->alias.'.field'])` or get the list of fields that would be retrived simply by `$queryData['fields']`. How to achieve this in cakephp 3.x ?

In beforeFind

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

the `options` is empty. The `$query` I can use to add conditions by `$query->where(...)`, but how to check what fields are set to be retrieved or what conditions are already applied ?

Thanks
Reply

#2
Taken from [the CakePHP 3.0 API documentation][1]:

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']);

So just call $query->traverse() and provide the callback closure and do your checks inside of it. See also [traverseExpressions()][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
Thanks for the hint, but method "traverse" in the 4th version I already did not work like this.
I looked at it in the Cake core:

public function beforeFind($event, $query, $options, $primary)
{
$query
->clause('where')
->iterateParts(function ($callable) use (&$params): void {
if ($callable->getField() === 'url') {
// do something
}
$params[] = [
$callable->getField() . ' ' . $callable->getOperator() => $callable->getValue()
];
});

$query->where($params);
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through