Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to build a 'and'-where query for relation
I hope I can describe my problem
Lets say I have the tables food, food_has_flags and flags.
== food ==
== food_has_flags ==
== flags ==
Now I try to get a orm query to find only food-rows with specific flags.
To find only one flag, there is no problem:
Food_Model::query()->related( 'flags', array(
'where' => array( array( 'id', '=', 2 ) )
But now I want to find all food, with the flags 1 and 2.
How can I do this?
I never use array notation, as it's limited to what it can do. This is an example, you can add multiple WHERE clauses, but only in an AND, not in an OR.
With chaining, you can:
I know that, but with chaining, i have to add the table name to the where, since the orm doesn't know which id is meant.
But even this, it doesn't solve my problem. For example:
->where(array( array( 'flags.id', '=', 2 ) ))
->_where( array( array( 'flags.id', '=', 1 ) ) )
Doesn't work, since the resulting query try to find an flags.id where whis is 2 and 1.
If i change to or, It's also wrong, since the where should be done for the join-tabe food_has_foodflags and not for the flags table.
Ok, i think its not possible with the orm system. Its a common situation, but I think I have to use DB:select.
But why it's not possible, to use SQL_CALC_FOUND_ROWS inside this? This version is faster than using the $resul->count() method.
You can use or_where() instead of where() if you need OR instead of AND.
If you want to use database expressions, you need to encapsulate them into DB::expr(), to prevent them from being escaped like a literal.
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Job Board
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion