Setup dynamic search in CakePHP - php

I am using the search plugin for CakePHP.
I try to dynamically setup the $filterArgs array that is used for filtering the input from the user. The reason why I want to do this is because my customers can create customer specific input fields. I want to make them filterable and searchable.
Customer.php:
public function beforeFind($queryData) {
$this->filterArgs['garantie'] = array(
'type' => 'subquery',
'method' => 'findCustomerCustomFieldsByText',
'field' => array('Customer.id'),
'encode' => true
);
}
Debugging $filterArgs shows that the entry was made:
array(
'garantie' => array(
'type' => 'subquery',
'method' => 'findCustomerCustomFieldsByText',
'field' => array(
(int) 0 => 'Customer.id'
),
'encode' => true
)
)
Unfortunately the method findCustomerCustomFieldsByText() is not called.
It looks like beforeFind() might not be the correct method to call. What callback-method should I use so I can use dynamic creation of $filterArgs?
Edit:
My question is not identical to the linked question because it is about the callback methods that prevent the filter from working.

Related

CakePHP - saveAssociated/saveAll behaves like saveMany

I have two Models, namely Product and ProductSpecification which have the following relations in place:
(Product Model)
public $hasMany = array(
'ProductSpecification' => array(
'className' => 'ProductSpecification',
'foreignKey' => 'product_id',
'dependent' => true
)
);
and
(ProductSpecification Model)
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id'
)
);
Using the CakePHP Form helper I post ProductSpecification data and then I use the saveAll method (or saveAssociated, I've tried both) to save the data. debug($this->request->data) gives me the following output after POSTing:
array(
'Product' => array(
'id' => '2'
),
'ProductSpecification' => array(
'title' => 'test',
'step' => '1',
'position' => '1'
)
)
This is great, right..? Now, the line after the debug I use the following code to save (I've also tried saveAssociated):
if($this->Product->saveAll($this->request->data))
For some odd reason this saves three(!) empty rows in my ProductSpecification table, with only the product_id field (and id) set; the fields title, step and position are empty. Exactly the same behavior happens when I run saveAssociated. What am I doing wrong?
I'm running CakePHP 2.x.
Your save data should look more like this:-
array(
'Product' => array(
'id' => '2'
),
'ProductSpecification' => array(
array(
'title' => 'test',
'step' => '1',
'position' => '1'
)
)
);
The values for ProductSpecification need to be passed as a numerically indexed array for the hasMany relationship.
Also, make sure you use saveAssociated() rather than saveAll() as you are passing associated data so there is no need to use the wrapper method (which should be avoided wherever possible).

Dynamically create the $filterArgs array in CakePHP

Is it possible to dynamically create the $filterArgs array in CakePHP when using the search plugin?
My customers are able to create their own input fields (customer specific) and I want to make all of them searchable. But for this I have to map them in the $filterArgs array.
E.g.:
public $filterArgs = array(
'input_filter' => array(
'type' => 'subquery',
'method' => 'findCustomerCustomFieldsByText',
'field' => 'Customer.id',
'encode' => true
)
);
Just add them conditionally as you need to the filterArgs array.
if ($someFieldIsPresentCheckHere) {
$this->Model->filterArgs['someThing'] = [ /* settings go here */ ];
}

Using CakeDC's search plugin to search data with multi-select field

I am trying to implement a search with CakeDC's Search plugin. In this search I have a field which has 'multiple' => 'checkbox' is set. This field allows user to select multiple cities so that they can filter results according to city/cities. What I did in favour to this, I simply specified the 'type' => 'IN' for that field in Searchable Model's $filterArgs. But noting happened it just responded with all result no searching/filtration happened. To get the clear picture of what I have implemented here are the code snippets:
Model.php
public $actsAs = array(
'Search.Searchable'
);
public $filterArgs = array(
'city' => array(
'type' => 'in',
'field' => 'Model.city'
));
search_form.ctp
echo $this->Form->create('Model', array('url' => array('controller' => 'models', 'action' => 'search')));
echo $this->Form->input('city', array(
'multiple' => 'checkbox',
'options' => array(
'city1' => 'city1',
'city2' => 'city2',
'cityn' => 'cityn')
));
echo $this->Form->end('search');
ModelsController.php
public function search() {
$this->layout = 'front_common';
$this->Prg->commonProcess();
$this->Paginator->settings = array(
'conditions' => $this->Model->parseCriteria($this->Prg->parsedParams()),
'limit' => 10
);
$this->set('Data', $this->Paginator->paginate());
}
also once I tried to use a beforeFilter() in ModelsController to implode the city array() with (,) to be used with IN but same all results. I want to ask if there is any other plugin to do this or any hack to do this with cakeDC's search plugin. Please help.
This should work fine, assuming you are passing in the arg to parseCriteria() as a simple array of values.
public $filterArgs = array(
array(
'name' => 'city',
'type' => 'value',
'field' => 'Model.city'
)
);
And you should be able to pass city:houston|dallas|austin in the URL
It should parse that into an array of args => [city => [houston, dallas, austin]]
And parseCriteria() will translate that into the following conditions fragment: [Model.city => [houston, dallas, austin]]
And CakePHP natively translates that into a SQL where fragment: IN(houston, dallas, austin)
Use DebugKit and monitor your SQL... you can debug() at any step of the process, you should get these values.
Here's the full docs on the Search plugin:
https://github.com/CakeDC/search/tree/master/Docs/Documentation
(I use it heavily, and I love how it lets me organize all search filters)

Lithium PHP framework - query parameters not passed to model ?

I work on a project, built with Lithium PHP framework and some other libraries like the "Resource" library. And so - I have a controller, that starts with:
<?php
namespace app\controllers\admin;
class Prices extends Base {
protected $_parameters = array(
'index' => array(
'prices' => array(
'required' => false,
'call' => array(
'all',
'conditions' => array(
'advertiser' => 'query:advertiser'
)
)
)
)
);
And the problem is that when I open the url with $_GET parameter advertiser - it is not passed to the model.
BUT - if I hardcode the advertiser ID like this:
...
'prices' => array(
'required' => false,
'call' => array(
'all',
'conditions' => array(
'advertiser' => '123'
)
)
)
and then I get the prices only for this advertiser - as I should.
What could be wrong? ...
You need to setup filter to achieve that, also natebele promised it will refactor li3_resources library to be more flexible:
Here is the link to the filter: https://gist.github.com/nateabele/5667381
It also includes simple use case in code comments if you need one.

codeigniter how can I get access to config/form_validation.php data?

I have duplicated data across my config/form_validation.php and my controller.
the field and label from form_validation is the same that I specify in my controller for the id, name and placeholder
Do I have to extract that data to yet a third location and reference it in both of these?
application/config/form_validation.php
$config = array(
'register' => array(
array(
'field' => 'register_username',
'label' => 'Username',
'rules' => 'trim|required|exact_length[5]'
),
.....
application/controllers/mycontroller.php
$this->viewdata['register_username'] = array(
'id' => 'register_username',
'name' => 'register_username',
'type' => 'text',
'placeholder' => 'Username'
);
...
I'm not sure why you have your data duplicated in the controller itself. You might find it easier to use Jamie Rumbelow's model/schema libraries mashup.
This will clean up your model/application structure as a whole. The model extension library itself allows for the automation of CRUD methods.

Categories