Doctrine ArrayCollection 拒绝在插入后更新

Doctrine ArrayCollection refuses to update after insert

我正在尝试保存 collection 个文件。

当我保存 collection 时,它会顺利进入数据库。与文件相同。 但是当我在同一个请求中添加一个 collection 和一个新文件时(就像在上传功能中一样)。 每当我要求教义给我 collection 中的文件时(在本例中为一个新文件)。 它总是以空的 ArrayCollection 响应。 如果我执行单独的 get HTTP 请求并在之后请求 collection,它会向我显示包含我的一个新文件的正确 arrayCollection。

我已经尝试了所有持久化和刷新实体的方法,以及更改级联注释的方法,但到目前为止没有任何效果。 我也试过清除学说缓存。

注释似乎是正确的,因为调用 getCollection()->getFiles() 会导致 ArrayCollection 包含链接到 collection 的文件。在创建两个实体并将它们链接在一起后,它似乎无法正常工作。

非常感谢您的帮助,代码在下面。

这是我的collection。其中包含作为 ArrayCollection 的文件。

/**
 * @Entity @Table(name="LH_FileCollections")
 **/
class LhFileCollection extends RootModel
{
    /**
     * @Column(type="string")
     */
    protected $title;

    /**
     * @OneToMany(targetEntity="LhFile", mappedBy="collection")
     */
    protected $files;

    //Getters and Setters

}

这是我的文件 class。

/**
 * @Entity @Table(name="LH_Files")
 **/
class LhFile extends RootModel
{
    /**
     * @Column(type="string")
     */
    protected $name;

    /**
     * @Column(type="string")
     */
    protected $type;

    /**
     * @Column(name="file_hash", type="string")
     */
    protected $fileHash;

    /**
     * @ManyToOne(targetEntity="LhFileCollection", inversedBy="files", cascade={"persist"})
     * @JoinColumn(name="collection_id", referencedColumnName="id")
     */
    protected $collection; 

    //Getters and Setters
}

这是我的存档collection功能。

/**
     * @return array|string
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Exception
     */
    public function fileUpload(
        $title,
        $attachment = null,
        $allowedFileTypes = null,
        $maxAllowedFileSize = 5000000
    ) {

        //Create collection
        $collection = $this->fileCollectionRepository->add($title);

        foreach ($_FILES as $file) {
            if ($allowedFileTypes !== null) {
                $errors = $this->fileCheck($file, $allowedFileTypes, $maxAllowedFileSize);
                if (!empty($errors)) {
                    return $errors;
                }
            }
            $this->saveFile($file, $collection);
        }

        return $collection;
    }

这是我的存档功能。

    /**
     * @param $file
     * @param $collection
     * @return LhFile
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    private function saveFile($file, $collection)
    {
        $currentDir = getcwd();
        $uploadDir = $currentDir . '/Data/Uploads/';
        $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
        $uniqueName = uniqid() . "_" . time() . '.' . $extension;

        move_uploaded_file($file['tmp_name'], $uploadDir . $uniqueName);

        $metaData = "...";

        $file = $this->fileRepository->add(
            $file['name'],
            $file['size'],
            $extension,
            $metaData,
            $uniqueName,
            $collection
        );

        return $file;
    }

最后是存储库功能:

文件collection:

/**
     * @param $fileCollection
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function add($title)
    {
        $fileCollection = new LhFileCollection();

        $fileCollection->setTitle($title);
        $this->em->persist($fileCollection);
        $this->em->flush();

        return $fileCollection;
    }

和文件

/**
     * @param $file
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function add($name, $size, $type, $metaData, $uniqueName, $collection)
    {
        $file = new LhFile();

        $file->setName($name);
        $file->setSize($size);
        $file->setType($type);
        $file->setMetadata($metaData);
        $file->setFileHash($uniqueName);
        $file->setCollection($collection);
        $this->em->persist($file);
        $this->em->flush();

        return $file;
    }

在我看来,在当前请求中,我可以确定文件没有被添加到 collection->files 数组中。数据库正在接收正确的关系,这就是为什么在第二次请求时它很好,但保存到数据库的行为不会自动填充关系。

我认为您需要明确地将文件添加到集合->文件中,假设您在 LhFileCollection 中有 getFiles,您可以在 $this->saveFile($file, $collection);:

之后添加
$collection->getFiles()->add($file);

当然有很多方法可以完成,但最终您需要将文件添加到集合->文件中。

就个人而言,我会建立集合,将每个文件添加到文件数组,然后才保存集合。我不会坚持并刷新每个文件,因为数据库操作可能很昂贵。你有级联所以它应该级联到所有文件。