Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

zend framework - Grouping WHERE clauses with Zend_Db_Table_Abstract

Does anyone know of a way to group where clauses with Zend_Db? Basically I have this query

$sql = $table->select()
             ->where('company_id = ?', $company_id)
             ->where('client_email = ?', $client_email)
             ->orWhere('client_email_alt = ?', $client_email);

Which is giving me this:

SELECT `clients`.* FROM `clients` WHERE (company_id = '1') AND (client_email = '[email protected]') OR (client_email_alt = '[email protected]')

But I need it to give me this, where the OR statement is grouped:

SELECT `clients`.* FROM `clients` WHERE (company_id = '1') AND ((client_email = '[email protected]') OR (client_email_alt = '[email protected]'))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to achieve this, you have to construct the grouped clause within a single call to the where method.

If both values of conditions are the same, you can do this:

$select->where('client_email = ? OR client_email_alt = ?', $client_email)

If there are multiple placeholders within the string, the DB adapter's quoteInto method will replace all placeholders with the provided value.

If you need to group an OR with different values for each field, you have to manually quote the values. It's a bit more complex:

$select->where(
    $db->quoteInto('client_email = ?', $email1) . ' OR ' . $db->quoteInto('client_email_alt = ?', $email2)
); // $db is your instance of Zend_Db_Adapter_*
   // You can get it from a Zend_Db_Table_Abstract 
   //subclass by calling its getAdapter() method 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...