How to perform complex searches and test them, in cakePHP and Simpletest
I have a very complex data structure, something like 10 tables without the join tables. My application needs to be able to perform search in most of the tables.
To do this, I though to turn the content of the search fields into an array of conditions. The key is the model name, the value is the search conditions, i.e.
$conditions = array(
'Artist' => array(
'OR' => array(
'Artist.name LIKE' => '%barl%',
'Pseudonym.name LIKE' => '%barl%'
)
),
'Content' => array('Content.subject' => 'architecture'),
'Editor' => array('Editor.name LIKE' => '%Gal%'),
etc....
)
This array gets passed to the models that are searchable and each model takes the condition that is relevant.
$this->find('all', array('conditions' => $conditions['Artist']))
So far so good, at least I think. Now I started to test the models and I found myself copying over and over that same array in the different model test cases, and that 开发者_JAVA百科bothers me.
Is there a way to have this array accessible to every test cases? Maybe the array is not the best solution and I should make a search model?
Any suggestions?
Put the array as property in the app_model.php as something like $commonSearchConditions and access them from inside your models which should inherit the AppModel.
Depending on what exactly you do, if the searches differ in every model, I would have a test-case in every models test. If not you might want to create a separate test with a test model you create inside of the test for testing just the search stuff you want to do. Hard to tell without knowing more.
精彩评论