第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Doctrine ArrayCollection 插入后拒絕更新

Doctrine ArrayCollection 插入后拒絕更新

PHP
瀟瀟雨雨 2023-09-22 15:22:22
我正在嘗試保存一組文件。當(dāng)我保存集合時(shí),它會(huì)順利進(jìn)入數(shù)據(jù)庫(kù)。與文件相同。但是當(dāng)我在同一請(qǐng)求中添加集合和新文件時(shí)(就像這里的上傳功能一樣)。每當(dāng)我要求教義為我提供集合中的文件(在本例中為一個(gè)新文件)時(shí)。它總是以空的 ArrayCollection 進(jìn)行響應(yīng)。如果我執(zhí)行單獨(dú)的 get HTTP 請(qǐng)求并隨后請(qǐng)求該集合,它會(huì)顯示包含我的一個(gè)新文件的正確 arrayCollection。我已經(jīng)嘗試了各種持久化和刷新實(shí)體的方法,以及更改級(jí)聯(lián)注釋,但到目前為止沒(méi)有任何效果。我也嘗試過(guò)清除教義緩存。注釋似乎是正確的,因?yàn)檎{(diào)用 getCollection()->getFiles() 會(huì)產(chǎn)生一個(gè)包含鏈接到該集合的文件的 ArrayCollection。創(chuàng)建兩個(gè)實(shí)體并將它們鏈接在一起后似乎無(wú)法正常工作。非常感謝您的幫助,代碼在下面。這是我的收藏。其中包含作為 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}這是我的文件類。/** * @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}這是我的保存文件收集功能。/**     * @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;    }
查看完整描述

1 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

在我看來(lái),在當(dāng)前的請(qǐng)求中,我可以看出文件沒(méi)有被添加到 collection->files 數(shù)組中。數(shù)據(jù)庫(kù)正在接收正確的關(guān)系,這就是為什么在第二個(gè)請(qǐng)求時(shí)沒(méi)有問(wèn)題,但保存到數(shù)據(jù)庫(kù)的行為不會(huì)自動(dòng)填充關(guān)系。

我認(rèn)為您需要將文件顯式添加到集合->文件中,假設(shè)您可以getFilesLhFileCollection之后添加$this->saveFile($file, $collection);

$collection->getFiles()->add($file);

當(dāng)然有多種方法可以完成,但最終您需要將文件添加到集合->文件中。

就個(gè)人而言,我會(huì)構(gòu)建集合,將每個(gè)文件添加到文件數(shù)組中,然后才保存集合。我不會(huì)堅(jiān)持并刷新每個(gè)文件,因?yàn)閿?shù)據(jù)庫(kù)操作可能會(huì)很昂貴。您已打開級(jí)聯(lián),因此它應(yīng)該級(jí)聯(lián)到所有文件。


查看完整回答
反對(duì) 回復(fù) 2023-09-22
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)