php - Doctrine2 是否应用过滤器来删除语句

标签 php symfony doctrine-orm

我使用原则过滤器,最近发现过滤器不适用于删除语句。我试图通过文档和谷歌进行挖掘,但谜团仍未解开。

例如,我有将用户连接到公司的过滤器,因此每个选择查询都像:

$userRepo->find(12);

修改自

SELECT .... FROM user t0 WHERE t0.id = 12

进入

SELECT .... FROM user t0 WHERE t0.id = 12 AND (t0.company_id = '6')

太棒了,这就是我需要的。

令我困扰的是 delete 语句似乎没有受到影响。有谁知道这是默认的学说架构还是我的配置有误?

过滤器

use Doctrine\ORM\Mapping\ClassMetaData;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\Common\Annotations\Reader;

class CompanyAware extends SQLFilter
{
    /**
     * @var Reader
     */
    protected $reader;

    /**
     * @param ClassMetaData $targetEntity
     * @param string $targetTableAlias
     *
     * @return string
     */
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        $query = '';
        $ann = 'Mrok\\PortalBundle\\Annotation\\CompanyAware';
        $isAware = $this->reader->getClassAnnotation($targetEntity->getReflectionClass(), $ann);

        if ($isAware) {
            $id = $this->getParameter('id');
            $query = sprintf('%s.company_id = %s', $targetTableAlias, $id);
        }

        return $query;
    }

    public function setAnnotationReader(Reader $reader)
    {
        $this->reader = $reader;
    }
}

最佳答案

由于 Doctrine Repositories 没有内置的 delete(id) 或 deleteBy(criteria),我假设您指的是 $em->remove($entity);或 DQL。查看代码(见下文),在执行 SQL 之前,Remove 或 cascade remove 都没有应用过滤器。文档指出应将过滤器应用于 DQL。

http://doctrine-orm.readthedocs.org/en/latest/reference/filters.html

/**
 * Deletes a managed entity.
 *
 * The entity to delete must be managed and have a persistent identifier.
 * The deletion happens instantaneously.
 *
 * Subclasses may override this method to customize the semantics of entity deletion.
 *
 * @param object $entity The entity to delete.
 *
 * @return void
 */
public function delete($entity)
{
    $class      = $this->class;
    $em         = $this->em;

    $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity);
    $tableName  = $this->quoteStrategy->getTableName($class, $this->platform);
    $idColumns  = $this->quoteStrategy->getIdentifierColumnNames($class, $this->platform);
    $id         = array_combine($idColumns, $identifier);
    $types      = array_map(function ($identifier) use ($class, $em) {

        if (isset($class->fieldMappings[$identifier])) {
            return $class->fieldMappings[$identifier]['type'];
        }

        $targetMapping = $em->getClassMetadata($class->associationMappings[$identifier]['targetEntity']);

        if (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])) {
            return $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type'];
        }

        if (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])) {
            return $targetMapping->associationMappings[$targetMapping->identifier[0]]['type'];
        }

        throw ORMException::unrecognizedField($targetMapping->identifier[0]);

    }, $class->identifier);

    $this->deleteJoinTableRecords($identifier);
    $this->conn->delete($tableName, $id, $types);
}

/**
 * @todo Add check for platform if it supports foreign keys/cascading.
 *
 * @param array $identifier
 *
 * @return void
 */
protected function deleteJoinTableRecords($identifier)
{
    foreach ($this->class->associationMappings as $mapping) {
        if ($mapping['type'] !== ClassMetadata::MANY_TO_MANY) {
            continue;
        }

        // @Todo this only covers scenarios with no inheritance or of the same level. Is there something
        // like self-referential relationship between different levels of an inheritance hierarchy? I hope not!
        $selfReferential = ($mapping['targetEntity'] == $mapping['sourceEntity']);
        $class           = $this->class;
        $association     = $mapping;
        $otherColumns    = array();
        $otherKeys       = array();
        $keys            = array();

        if ( ! $mapping['isOwningSide']) {
            $class       = $this->em->getClassMetadata($mapping['targetEntity']);
            $association = $class->associationMappings[$mapping['mappedBy']];
        }

        $joinColumns = $mapping['isOwningSide']
            ? $association['joinTable']['joinColumns']
            : $association['joinTable']['inverseJoinColumns'];


        if ($selfReferential) {
            $otherColumns = (! $mapping['isOwningSide'])
                ? $association['joinTable']['joinColumns']
                : $association['joinTable']['inverseJoinColumns'];
        }

        foreach ($joinColumns as $joinColumn) {
            $keys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
        }

        foreach ($otherColumns as $joinColumn) {
            $otherKeys[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
        }

        if (isset($mapping['isOnDeleteCascade'])) {
            continue;
        }

        $joinTableName = $this->quoteStrategy->getJoinTableName($association, $this->class, $this->platform);

        $this->conn->delete($joinTableName, array_combine($keys, $identifier));

        if ($selfReferential) {
            $this->conn->delete($joinTableName, array_combine($otherKeys, $identifier));
        }
    }
}

关于php - Doctrine2 是否应用过滤器来删除语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429599/

相关文章:

php - 使用 Javascript 打开两个新选项卡

c# - 将 'bitmap' 从 C# 上传到 PHP

controller - Symfony: Controller 工厂

mysql - SQLSTATE[28000] [1045] 拒绝用户 'root' @'localhost' 的访问(使用密码 : YES) Symfony2

php - 如何根据相关OneToMany关联中的条件选择一组实体?

php - 数据库 mysql_num_rows() 期望参数 1 是资源, bool 值在

PHP MySQLi fatal error : Call to a member function fetch_array() on a non-object in

php - Symfony 2.7.4 Twig 错误

mysql - 具有计算间隔值的 Doctrine 2 DATE_ADD mysql 函数

php - Symfony2 - 数据库级别 onDelete Cascade 不会触发生命周期事件