Hi Josh,
Thanks for the answer. Let me explain what I am trying to achieve.
I am working on filter records based on data entered on search form.
---Controller code----
$this->paginate = ['searchFilter'=>true, 'limit'=>10,
'order'=>['Users.id'=>'DESC'], 'conditions'=>['Users.active'=>1]];
$users = $this->paginate('Users')->toArray();
$this->set(compact('users'));
----View code----
Here are search form fields for example First Name, Last Name, Email,
Status("", Active, Inactive) etc these should be anything based on
configuration
[Search][Reset] buttons
Users data in HTML table
----Searchable Behaviour----
FYI- In Search Component I attach a Search behaviour with current model
table and pass predefined Search Form Fields(same we displayed in view) to
behaviour as config.
function beforeFind(Event $event, Query $query, $options, $primary) {
// here I have Search Form Fields and based on 'searchFilter' option in
$options array I can modify only desired Query.
Case 1-
I can make a new condition array based on search form fields. and call
$query->where($cond);
This way it adds new conditions in existing conditions in query object.
Failing Situation.
Existing condition has Users.active = 1 and If new condition array has
Users.active = 0 so this way merged conditions will have Users.active = 1
AND Users.active = 0 //No Result
Case 2-
I can make a new condition array based on search form fields. and call
$query->where($cond, [], ture);
This way it overwrite exiting conditions with new conditions in query
object.
Failing Situation.
Existing condition has Users.active = 1 and If new condition array *has not*
Users.active
field so this way wrong result comes, means inactive users also comes. //in
this case original condition should preserve.
// What I want solution is If some how I get existing condition as an array
then I can modify them and can call
$query->where($cond, [], ture);
//As we do in cakephp 2.x.
return $query;
}
I hope it's clear to you. Please let me know if you want any more
clarification.
Thanks
On Friday, January 16, 2015 at 12:47:39 AM UTC+5:30, José Lorenzo wrote:
>
> Even though that is possible you are tying to do it the hard way. The
> easiest way you can implement the same is by using the query as a "command"
> object,
> thus instructing the custom finders to do stuff based on the passed
> options. An example
>
> $table->find('forUser', ['user' => $user])->find('withStatus', ['status'
> => 'active']);
>
> Then in your table you would create your finders this way:
>
> public function finWithStatus($query, $options) {
> if (!empty($options['user'])) {
> // The user is available from the previous finder, I can add more
> conditions here based on that
> }
> }
>
>
> I guess the best way I can help you find the best strategy is if you
> mention what exactly you are trying to do :D
> On Thursday, January 15, 2015 at 3:26:09 PM UTC+1, Cake Developer wrote:
>>
>> Hello,
>>
>> I am trying to get and modify query conditions in beforeFind function of
>> Behaviour.
>>
>> function beforeFind(Event $event, Query $query, $options, $primary) {
>> // here is what I tried so far.
>> $conditions = $query->clause('where');
>> // this gives me Object of Expression Interface and I am not able to
>> convert it in condition array like we have in cakephp 2.x
>>
>> //what I want to achieve...
>> //I want to modify existing conditions in query.
>> //I want to add new condition in query
>>
>> }
>>
>> I spent last 2 days but could not figure out any solution.
>> Thanks for any help you may be able to provide regarding this.
>>
>>
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.