douao1858 2015-05-24 23:57
浏览 21
已采纳

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

I use doctrine filters and recently noticed that filters are not applied to delete statements. I have tried to dig through documentation and google, but mystery stays unsolved.

For example I have filter which connects user to company so every select query like:

$userRepo->find(12);

and is modified from

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

into

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

cool, that is what I need.

What bothers me is that delete statements seems to be untouched. Does anyone know if it is default doctrine architecture or my configuration is wrong?

The filter

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;
    }
}
  • 写回答

1条回答 默认 最新

  • dongrui6787 2015-05-25 12:14
    关注

    As Doctrine Repositories doesn't have a delete(id) or deleteBy(criteria) as a built-in, I assume you are referring to either $em->remove($entity); or DQL. Looking at the code (see below) neither Remove or cascade remove apply filters before executing the SQL. The documentation indicates that filters should be applied to 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));
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图