CakePHP Auto Magic Search Conditions
Hey folks, so this is a nifty trick I implemented after being inspired from overriding CakePHP Model::find(). There's nothing to it really, just making use of the extreme flexibility CakePHP's architecture allows, provided you wrap your head around the CakePHP way of doing things
Before commencing, I would really advise anyone out there who wants to make it to a good level in CakePHP to read Matt's book. To make this post shorter I will assume that you fully understand how to override Model::find() for CakePHP as Matt explains in his book as "My Way". These are the functions that you will need:
class AppModel extends Model { public $searchValues = null; } return $this->{$method}($options); } public function searchConditions() { foreach($this->searchValues as $val) { $conds = am($conds, $_conds); } return $conds; } } class User extends AppModel { $age = Set::classicExtract($this->searchValues, 'age'); $ageComp = Set::classicExtract($this->searchValues, 'age_comparison'); ); $conds = $ageConds[$ageComp]; } return $conds; } } class UsersController extends AppController { public function search() { if ($this->RequestHandler->isPost() || $this->RequestHandler->isPput()) { $this->User->searchValues = $this->data['User']; $conditions = $this->User->searchConditions(); } } }
Hold on ... don't panic, I assure you it's quite simple
- First off, the AppModel::searchCondition() will generate the condition for you and you would need to write up the proper conditions within the model itself which is the User model in our case.
As you will notice that I am trying to find the "age" of the particular user. I only have the User's date of birth stored as "dob" field in the users table. There is NO "age" field in the users table of course. So in essence, you may or may not relate actual field names of the table. This is what makes it really powerful. And this is what can allow me to find a User's age via more than 1 way. This is a very simple demonstration what can be done. For reasons of simplicity I have not used the $options variable which you will see as the second parameter of the AppModel::searchCondition(). In the code above you will see only the 'conditions' being returned, in my actual implementations I return all the options of a normal Model::find() call. That allows me to set joins and any virtual fields I like. It's pretty nifty once you dwell on the whole process.
I can simply write up another search condition filter in my User model,
$age = Set::classicExtract($this->saerchValues, 'order_than'); } return $conds; }
And this would work if I supply a valid numeric value in "older_than" field I can create on the fly using the FormHelper in my form so this would be supplied as Controller::data['User']['older_than'] in the controller.
You can easily follow this concept and play with the code to also include all the options in the Model::find() call as I've mentioned above. When you do this, retrieving data sets becomes a breeze ! ... It's like you tame your model to behave the way you want whenever the particular fields are given !
P.S. shoot ! ... so this post I've tried to put together under too many distractions
... I hope I make it clear though.
Recent Comments