Last active
December 28, 2015 00:59
-
-
Save chanmix51/7417564 to your computer and use it in GitHub Desktop.
Custom queries using Pomm 1.2
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 // CustomerMap.php | |
// ... | |
public function findAllWithOrderCount() | |
{ | |
return $this->queryWithOrderCount(new Where()); | |
// SELECT ... WHERE true; | |
} | |
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 // CustomerMap.php | |
// ... | |
public function findByPKWithOrderCount($customer_id) | |
{ | |
return $this->queryWithOrderCount(new Where('customer_id = $*', array($customer_id)))->current(); | |
} | |
protected function queryWithOrderCount(Where $where) | |
{ | |
$order_map = $this->connection->getMapFor('\App\Shop\Order'); | |
$sql = <<<SQL | |
SELECT | |
:customer_fields_cu, | |
count(order.*) AS order_count | |
FROM | |
:customer_table cu | |
LEFT JOIN :order_table order ON order.customer_id = cu.customer_id | |
WHERE | |
:where | |
SQL; | |
$sql = strtr($sql, array( | |
':customer_fields_cu' => $this->formatFieldsWithAlias('getSelectFields', 'cu'), | |
':customer_table' => $this->getTableName(), | |
':order_table' => $order_map->getTableName(), | |
':where' => (string) $where | |
) | |
); | |
return $this->query($sql, $where->getValues()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment