Multiple / nested "select where" with Zend_Db_Select

Possible duplicate:
Grouping WHERE clauses with Zend_Db_Table_Abstract

I need to create something like this:

select name from table where active = 1 AND (name LIKE 'bla' OR description LIKE 'bla') 

The first part is simple:

 $sqlcmd = $db->select() ->from("table", "name") ->where("active = ?", 1) 

Now comes the hard part. How can I nest? I know that I can just write

 ->orWhere("name LIKE ? OR description LIKE ?", "bla") 

But this is wron, because I need to dynamically change all parts. The request will be built all the time when the script is run. Some parts are deleted, some are changed. In this example, I need to add these OR-s, because sometimes I need to look for a wider one. "My Zend Logic" tells me that the correct way is:

 $sqlcmd = $db->select() ->from("table", "name") ->where("active = ?", 1) ->where(array( $db->select->where("name LIKE ?", "bla"), $db->select->orWhere("description LIKE ?", "bla") )) 

But this does not work (at least I do not remember that it worked).

Please . Can someone help me find an object oriented way to nest "where" -s

+4
source share
2 answers

Here is an example from the ZF manual

  // Build this query: // SELECT product_id, product_name, price // FROM "products" // WHERE (price < 100.00 OR price > 500.00) // AND (product_name = 'Apple') $minimumPrice = 100; $maximumPrice = 500; $prod = 'Apple'; $select = $db->select() ->from('products', array('product_id', 'product_name', 'price')) ->where("price < $minimumPrice OR price > $maximumPrice") ->where('product_name = ?', $prod); 

It should fit your needs.

+1
source

To create this query:

SELECT product_id, product_name, price FROM "products" WHERE (price > 100.00) AND (price < 500.00)

use this code:

 $minimumPrice = 100; $maximumPrice = 500; $select = $db->select() ->from('products', array('product_id', 'product_name', 'price')) ->where('price > ?', $minimumPrice) ->where('price < ?', $maximumPrice); 
-3
source

Source: https://habr.com/ru/post/1309205/


All Articles