2013-07-07 13 views
6

Nel mio modello:In ZF2 come eseguire o condizione nella clausola WHERE

$rowset = $this->tableGateway->select(array('username' => $identifier)); 
$row = $rowset->current(); 
return $row; 

Si esegue la seguente query:

SELECT * FROM member WHERE username='<< ENTERED VALUE >>'; 

ma voglio eseguire la query seguente:

SELECT * FROM member WHERE username='<< ENTERED VALUE >>' OR id_customer='<< ENTERED VALUE >>'; 

Quali sono le modifiche che devo apportare nel file di modello?

E per favore suggerisci un blog utile a riguardo. Non riesco a trovare una risposta per questo in ZF2 Documentation.

risposta

4

Il modo più semplice per farlo è usare l'esplicito OR parola chiave:

$where = new Zend\Db\Sql\Where; 
$where->equalTo('username', $identifier); 
$where->OR->equalTo('id_customer', $customerId); 

$rowset = $this->tableGateway->select($where); 
$row = $rowset->current(); 
return $row; 
2

Ho più esperienza con ZF 1 che con ZF 2 quindi ci potrebbero essere altri (meglio più semplici, soluzioni), ma questo dovrebbe fare il trucco:

// Manually build the Select object 
$select = $this->tableGateway->getSql()->select(); 

// Create array containing the fields and their expected values 
$conditions = array('username' => 'foo', 'id_customer' => 123); 

// Add these fields to the WHERE clause of the query but place "OR" in between 
$select->where($conditions, \Zend\Db\Sql\Predicate\PredicateSet::OP_OR); 

// Perform the query 
$rowset = $this->tableGateway->selectWith($select); 
3

un po 'tardi alla festa, ma per completezza ho pensato di fornire un'alternativa che ha funzionato abbastanza bene per me, e uno che potrebbe essere un po' più facile da implementare per alcuni sviluppatori :

// '$gateway' is a Zend\Db\TableGateway\TableGateway object... 
$search_string = 'something'; 
$select = $gateway->select(function($select) use($search_string) { 
    $select->where->OR->like('first_name', '%'. $search_string .'%'); 
    $select->where->OR->like('last_name', '%'. $search_string .'%'); 
}); 

Dopo aver eseguito ciò, $select manterrà il set di risultati, pronto per il collegamento.

Spero che questo aiuti qualcuno! :)

Problemi correlati