Last active
September 28, 2015 03:48
-
-
Save chanmix51/1380215 to your computer and use it in GitHub Desktop.
Using the where class to dynamically build your query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
$cars = $database | |
->createConnection() | |
->getMapFor('YourDb\Garage\Car') | |
->getSportAndColors(array('blue', 'red', 'yellow')) | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using_where2.php
look like acontroller.php
thinks it's wrong?