sql - CakePHP query additions in controller -


I am migrating raw PHP code to KHHHP and there are some problems like I have problems with the question of ORM conversion I use temporary raw sql. I is doing everything well, but i got ugly code and in fact does not know how to make it beautiful i dealerscontroller and added function Advanced ($ condition = null) (this is 1 to 15 more parameters And will be asked to AJAX with parameters). The function looks like:

  switch ($ bet) {case '1': $ cond_query = 'AND ((d.email = \' \ 'or d.email IS is tap) ); break; Case '2': $ cond_query = 'AND (dealer_id FROM dealer_logo)'; break; // In many cases, some are long, some are like these two if ($ user_group == 'group_1') { $ Query = 'Long term with hosting up to 6+'. $ Cond_query;} Otherwise ($ user_group == 'group_2') {$ query = 'Table 6+ long long search with a long bit different long search'. $ Cond_query;} Other {$ query = '10+ table plus a little more bit different long search with '$ .condition_query;} // this is $$ - dealer- & gt; query ($ query); and so on   

So .. as you see the code looks ugly Is. I have two types:

1) In addition to the query to get out and make way model for every condition, then separated to work on these situations. But this is not dry, because the main 3 major queries are almost identical and if I have to change something - I'll need to change the 16+ questions.

2) Take a small reusable model methods / queries to exit data from small pieces of DB, then do not use raw SQL, but play along the way. It would be nice, but the performance will be reduced and I will need it as high.

Please advise me thank you!

If you are concerned that KPHHP creates a database query for each matching table, Then you might be that this behavior can help you reduce the number of queries (where there are easy associations to be included in a table).

Otherwise, I think that to get the usual database queries method at the model level to get the small pieces of information, and then adding them, then there is a good way to get your code explicitly (Via inline documentation) if you can migrate to using the Find methods of KPHPH instead of raw queries, you can set the condition Not use array syntax. So in a way you can withstand your problem that public functions are done on your ideal sections, which attach them in the condition of suitable conditions. For example:

  class SomeModel extends AppModel {... public function addEmailCondition (& amp; $ terms) {$ terms ['or'] = array ('alias.email_address '= & Gt; null,' Alias.email_address = '=>;' '); }}   

You ask these actions to create a large conditions array, which you can use to retrieve data from your controller (Or models if you want to put it on the model layer). Note that in the above example, the conditions array is being passed by reference, so it can be edited in place. Also keep in mind that any existing 'OR' conditions present in the array This function will be overwritten by: With your existing solutions, your real solution to be smart is to merge your new statuses.

Do not worry about 'fictitious' performance issues - if you have tried the questions and they are very slow, then you can worry about how to increase performance But for beginners, try writing code as much as possible.

You may also want to consider splitting the number of controller operations called function advanced () by their condition query equality.

Finally, if you have not checked it already, then here is the entry of the book on recovering data from the models. There may be some tips you have not seen before:

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -