I've got a little task I've gotta handle, and while I could just use the query()
function write out the SQL by hand, I'm really interested in learning about this.
NOTE: I'm pretty green on ZF (especially as a framework) .. I'm just using DB as a lib.
Desired Outcome:
What I'd like my sql to look like is something like:
SELECT name, phone, email
FROM company
WHERE name = 'Acme Anvil Corp'
AND ( category1 = 'abc'
OR category3 = 'klm'
OR category8 = 'xyz'
)
Where I've gotten so far:
$select = $db
->select()
->from('company, array('name', 'phone', 'email'))
->where('name = ?', 'Acme Anvil Corp')
->limit(1);
After that, I'm buggered.
Adding a whole slew of orWhere()
's jut won't give the desired outcome.
How can I add a bunch of orWhere()
's in a collective grouping ()
?
Alternatively, I thought about trying to go the route of aiming for my SQL to look something more like:
SELECT c.name, c.phone, c.email
FROM (
SELECT name, phone, email
FROM company
WHERE category1 = 'abc'
OR category3 = 'klm'
OR category8 = 'xyz'
) AS c
WHERE c.name = 'Acme Anvil Corp'
(I'm no SQL guru, so if there's a significant difference between these two, don't be shy on the feedback :)
Again, just unsure about how to approach that.
use Zend\Db\Sql\Predicate;
$select->where(array(
// ...
new Predicate\PredicateSet(
array(
new Predicate\Like('content', '%'.$searchstring.'%'),
new Predicate\Like('title', '%'.$searchstring.'%'),
),
Predicate\PredicateSet::COMBINED_BY_OR
),
// ...
));
Also,
$predicate = new \Zend\Db\Sql\Where();
$sql->where($predicate->greaterThan('filterColumn', '20'));
$sql->where($predicate->greaterThan('filterColumn', '30'), 'OR');