doushu5451 2014-01-22 11:27
浏览 28

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

Referring to Symfony 2's Cookbook about file upload, I tried to use Doctrine's @PostRemove event listener to remove a file after the file has been removed from database.

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

Database:

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

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

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

When I remove a Document with Id 1, somehow doctrine unlink the File with Id 1 as well.

From what I can found out, this strange behavior occurs as a result of these steps :

1- Doctrine's UnitOfWork will call out the commit() method which in turn calls to executeDeletions()

2- In executeDeletions() the persister deleted the Document's File according to it's id and then do

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

which sets the File's id value to null, then it starts to call it's events

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

3- Since $file property in Document model is a one-to-one relation, Doctrine automatically creates a File proxy class in place of the real File class as Document's property.

4- While invoking the removeFile() function it calls to FileProxy's getAbsolutePath() :

FileProxy.php

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

    return parent::getAbsolute();
}

Which invokes the initializer with closure:

    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- Calling the $entityPersister->load($classMetadata->getIdentifierValues($proxy), $proxy) calls to EntityPersister's

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's load() will then hydrate the FileProxy with the last item in it's query result, which is the File with Id 1, since according to the current transaction File with Id 2 is already deleted.

I got around this by using fetch=EAGER in Document's $file mapping, but I'm curious about this issue.

Did I do something wrong, is this an expected behavior, or a bug perhaps?

  • 写回答

1条回答 默认 最新

  • douyu5679 2018-08-01 08:50
    关注

    fetch=EAGER works fine but in our case we had a lot of entities with dependency on Image entity so instead of adding fetch=EAGER in all of them we added initialization in 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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法