Commit d909a9cb authored by Tomas Lang's avatar Tomas Lang

Entity: getCountOf* method

parent 1d867a92
...@@ -2,7 +2,47 @@ ...@@ -2,7 +2,47 @@
namespace LeanMapperWorkflow; namespace LeanMapperWorkflow;
use LeanMapper\Relationship\HasMany;
use LeanMapper\Filtering;
use LeanMapper\Fluent;
abstract class Entity extends \LeanMapper\Entity abstract class Entity extends \LeanMapper\Entity
{ {
/**
* @param string $name
* @param array $arguments
*/
public function __call($name, array $arguments)
{
if (substr($name, 0, 10) === 'getCountOf')
{
$property = $this->getCurrentReflection()->getEntityProperty(lcfirst(substr($name, 10)));
if ($property->containsCollection())
{
$relationship = $property->getRelationship();
$table = $relationship instanceof HasMany ? $relationship->getRelationshipTable() : $relationship->getTargetTable();
$column = $relationship->getColumnReferencingSourceTable();
$filters = array('counter');
$propertyFilters = $property->getFilters();
if ($propertyFilters !== NULL) {
$filters = array_merge($filters, $propertyFilters);
}
$filtering = new Filtering(function(Fluent $statement) use ($column) {
$statement->removeClause('select')->select('COUNT(*) [count], %n', $column)
->groupBy($column);
});
$rows = $this->row->referencing($table, $column, $filtering);
return empty($rows) ? 0 : $rows[0]->count;
}
}
return parent::__call($name, $arguments);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment