In ZF2, how to fulfill an OR clause in a WHERE clause

In my model:

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

It executes the following query:

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

but I want to execute the following query:

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

What changes should I make to the model file?

And please suggest a useful blog about this. I can not find the answer for this in the ZF2 documentation.

+6
source share
3 answers

The easiest way to do this is to use the explicit OR keyword:

 $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; 
+4
source

A little late for the party, but for the sake of thoroughness, I thought that I would provide an alternative that would work well for me, and one that might be a little easier to implement for some developers:

 // '$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 .'%'); }); 

Once launched, $select will hold your result set, ready to go through.

Hope this helps someone! :)

+3
source

I have more experience with ZF 1 than with ZF 2, so there may be other (better, simpler) solutions, but this should do the trick:

 // 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); 
+2
source

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


All Articles