Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active September 28, 2015 03:48
Show Gist options
  • Save chanmix51/1380215 to your computer and use it in GitHub Desktop.
Save chanmix51/1380215 to your computer and use it in GitHub Desktop.
Using the where class to dynamically build your query
<?php
//...
$cars = $database
->createConnection()
->getMapFor('YourDb\Garage\Car')
->getSportAndColors(array('blue', 'red', 'yellow'))
;
<?php
namespace MyDatabase\Garage;
use MyDatabasey\Garage\Base\CarMap as BaseCarMap;
use Pomm\Exception\Exception;
use Pomm\Query\Where;
class CarMap extends BaseCarMap
{
protected function getWhereForColors(Array $colors)
{
$where = new Where();
foreach($colors as $color)
{
$where->orWhere('color = ?', array($color));
}
return $where;
}
public function getSportAndColors($colors)
{
$where = Where::create('type = ?', array('sport'))
->andWhere($this->getWhereForColors($colors));
return $this->findWhere($where);
}
}
<?php
namespace MyDatabase\Garage;
use MyDatabase\Garage\Base\CarMap as BaseCarMap;
use Pomm\Exception\Exception;
use Pomm\Query\Where;
class CarMap extends BaseCarMap
{
public function getSportAndColors(Array $colors)
{
return $this->findWhere(Where::createWhereIn('color', $colors)->andWhere('type = ?', 'sport'));
}
}
@chanmix51
Copy link
Author

Problem with my vim Gist plugin. The createWhereIn example is back, thx a lot @pilot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment