6

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.

4

3 回答 3

4

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;
于 2013-07-08T09:24:45.900 回答
3

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! :)

于 2013-10-01T19:25:28.557 回答
2

I've more experience with ZF 1 than with ZF 2 so there might 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);
于 2013-07-07T11:31:12.963 回答