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

标签 php mysql symfony doctrine-orm doctrine

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

文档.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;
    }
}

文件.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- Doctrine 的 UnitOfWork 将调用 commit() 方法,该方法又调用 executeDeletions()

2-在executeDeletions()中,持久化器根据文档的id删除了文档的文件,然后执行

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() 然后将使用其查询结果中的最后一项(即 Id 1 的文件)来水合 FileProxy,因为根据当前事务,Id 2 的文件已经存在已删除。

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

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

最佳答案

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);
}

关于php - Symfony Doctrine 2 PostRemove 删除文件奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281659/

相关文章:

php - 如何使用 PHP 将 MySQL 时间转换为 ISO 8601 持续时间

php - PHP 中 mysql 查询字符串的奇怪行为

mysql - 使用 MySQL Regex 查找和替换

javascript - 如何在 Symfony 中将 PHP session 变量传递给外部 JS 文件

javascript - 如何在 Controller 操作 Symfony 2 中获取表单

php - Doctrine 仅返回空缓存的结果

php - 如何将 prestashop 与 android 集成?

php - PHP 函数 fwrite 中的 IF THEN 表达式

php - 如何从数据库中选择日、周、月、年的信息?

php - 查找给定位置的记录总数