Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ZF2 how to execute OR condition in 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 execute the following query:

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

What are the changes I have to make in model file?

And please suggest useful blog regarding this. I cant find answer for this in ZF2 Documentation.

like image 245
vimal1083 Avatar asked Jul 07 '13 10:07

vimal1083


2 Answers

The easiest way to do so is using 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;
like image 177
Timothee A Avatar answered Sep 28 '22 17:09

Timothee A


A little late to the party, but for thoroughness I thought I'd provide an alternative that worked quite 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 .'%');
});

After running that, $select will be holding your result set, ready to loop through.

Hope this helps someone! :)

like image 22
Chris Kempen Avatar answered Sep 28 '22 19:09

Chris Kempen