Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active December 28, 2015 00:59
Show Gist options
  • Save chanmix51/7417564 to your computer and use it in GitHub Desktop.
Save chanmix51/7417564 to your computer and use it in GitHub Desktop.
Custom queries using Pomm 1.2
<?php // CustomerMap.php
// ...
public function findAllWithOrderCount()
{
return $this->queryWithOrderCount(new Where());
// SELECT ... WHERE true;
}
<?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