Symfony Doctrine 2 PostRemove删除文件奇怪的行为

时间:2014-01-22 11:27:41

标签: php mysql symfony doctrine-orm doctrine

参考Symfony 2关于文件上传的Cookbook,我尝试使用Doctrine的@PostRemove事件监听器在文件从数据库中删除后删除文件。

Document.php

/** @Entity */
class Document {
    /** OneToOne(targetEntity="File", cascade={"all"}) */
    private $file;

    public function setFile(File $file) {
        $this->file = $file;
    }

    public function getFile() {
        return $this->file;
    }
}

File.php

/*
** @Entity
** @HasLifecycleCallbacks
*/
class File extends {
    /** @Column(type="string") */
    private $name;

    public function __construct(UploadedFile $file) {
        $this->path = $file->getPathname();
        $this->name = $file->getClientOriginalName();
    }

    public function getAbsolute() {
        return '/var/www/cdn.myweb.com/file/'.$this->name;
    }

    /** @PostRemove */
    public function removeFile() {
        unlink($this->getAbsolute());
    }
}

数据库:

**Document**
--------
|  id  |
-------
|   1  |
--------

**DocumentFiles**
--------------------------
| document_id | file_id  |
--------------------------
|      1      |     2    |
--------------------------

**File**
--------------------
|  id  |   name    |
--------------------
|   1  | file1.ext |
|   2  | file2.ext |
--------------------

当我删除带有Id 1的文档时,不知何故,学说也将文件与Id 1取消链接。

据我所知,这些奇怪的行为是由于以下步骤而发生的:

1-教条的UnitOfWork将调出commit()方法,后者又调用executeDeletions()

2-在executeDeletions()中,persister根据它的id删除了Document的文件,然后执行

if ( ! $class->isIdentifierNatural()) {
    $class->reflFields[$class->identifier[0]]->setValue($entity, null);
}

将文件的id值设置为null,然后开始调用它的事件

if ($invoke !== ListenersInvoker::INVOKE_NONE) {
    $this->listenersInvoker->invoke($class, Events::postRemove, $entity, new LifecycleEventArgs($entity, $this->em), $invoke);
}

3-由于Document模型中的$ file属性是一对一的关系,因此Doctrine会自动创建一个File代理类来代替真正的File类作为Document的属性。

4-调用removeFile()函数时调用FileProxy的getAbsolutePath()

FileProxy.php

public function getAbsolute() {
    $this->__initializer__ && $this->__initializer__invoke($this, 'getAbsolute', array());

    return parent::getAbsolute();
}

使用闭包来调用初始化程序:

    function (BaseProxy $proxy) use ($entityPersister, $classMetadata) {
        $initializer = $proxy->__getInitializer();
        $cloner      = $proxy->__getCloner();

        $proxy->__setInitializer(null);
        $proxy->__setCloner(null);

        if ($proxy->__isInitialized()) {
            return;
        }

        $properties = $proxy->__getLazyProperties();

        foreach ($properties as $propertyName => $property) {
            if (!isset($proxy->$propertyName)) {
                $proxy->$propertyName = $properties[$propertyName];
            }
        }

        $proxy->__setInitialized(true);

        if (null === $entityPersister->load($classMetadata->getIdentifierValues($proxy), $proxy)) {
            $proxy->__setInitializer($initializer);
            $proxy->__setCloner($cloner);
            $proxy->__setInitialized(false);

            throw new EntityNotFoundException();
        }
    };

5-调用EntityPersister的$entityPersister->load($classMetadata->getIdentifierValues($proxy), $proxy)次调用

public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0, $limit = null, array $orderBy = null)
{
    $sql = $this->getSelectSQL($criteria, $assoc, $lockMode, $limit, null, $orderBy);
    list($params, $types) = $this->expandParameters($criteria);
    $stmt = $this->conn->executeQuery($sql, $params, $types);

    if ($entity !== null) {
        $hints[Query::HINT_REFRESH]         = true;
        $hints[Query::HINT_REFRESH_ENTITY]  = $entity;
    }

    $hydrator = $this->em->newHydrator($this->selectJoinSql ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT);
    $entities = $hydrator->hydrateAll($stmt, $this->rsm, $hints);

    return $entities ? $entities[0] : null;
}

6- EntityPersister的load()将使FileProxy与其查询结果中的最后一项保持一致,即具有Id 1的文件,因为根据当前事务,已删除带有Id 2的文件。

我在Document的$ file映射中使用fetch=EAGER解决了这个问题,但我很好奇这个问题。

我做错了什么,这是预期的行为,还是错误?

1 个答案:

答案 0 :(得分:0)

fetch = EAGER可以正常工作,但是在我们的例子中,我们有很多依赖于Image实体的实体,因此我们没有在所有实体中添加fetch = EAGER,而是在preRemove中添加了初始化

/**
 * @param Image $image
 * To ensure object initialization, path required in postRemove method
 */
public function preRemove(Image $image)
{
    $image->getPath();
}

/**
 * @param Image $image
 */
public function postRemove(Image $image)
{
    $imagePath = $this->imageHelper->getImagePath($image);

    $this->filesystem->remove($imagePath);
}