Skip to content

Instantly share code, notes, and snippets.

@beberlei
Created July 21, 2011 10:46
Show Gist options
  • Save beberlei/1096947 to your computer and use it in GitHub Desktop.
Save beberlei/1096947 to your computer and use it in GitHub Desktop.
First idea for SQL Filtering in Doctrine2
<?php
class MySoftDeleteFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias, Connection $conn)
{
if ($targetEntity->name != "MyEntity\SoftDeleteNewsItem") {
return "";
}
return ' AND '.$targetTableAlias.'.deleted = 0';
}
}
class MyLocaleFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
if (!in_array("LocaleAware", $targetEntity->reflClass->getInterfaceNames())) {
return "";
}
return ' AND ' . $targetTableAlias.'.locale = ' . $this->getParam('locale'); // getParam uses connection to quote the value.
}
}
$config->addFilter("locale", "MyLocaleFilter");
$config->addFilter("soft_delete", "MySoftDeleteFilter");
$em = EntityManager::create($params, $config);
$em->enableFilter("locale")->setParameter("locale", "en");
<?php
namespace Doctrine\ORM\Query\Filter;
abstract class SQLFilter
{
final public function __construct(Connection $conn);
final function setParameter($name, $value, $type);
final function getParameter($name);
abstract function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias);
}
namespace Doctrine\ORM;
class Configuration
{
public function addFilter($name, $className);
public function getFilterClassName($name);
}
class EntityManager
{
private $enabledFilters;
/** @return SQLFilter[] */
public function getEnabledFilters();
/** Throws exception if filter does not exist. No-op if the filter is alrady enabled.
* @return SQLFilter */
public function enableFilter($name);
/** Disable the filter, looses the state
* @return SQLFilter */
public function disableFilter($name) { unset($this->enabledFilters[$name]); }
/** throws exception if not in enabled filters
* @return SQLFilter */
public function getFilter($name);
}
class Query
{
// needs to serialize the enabled filters + their parameters for the DQL Hash to SQL Cache Computation.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment