Doctrine2反向持久性在嵌套表单中不起作用 [英] Doctrine2 inverse persistance not working in nested forms

查看:129
本文介绍了Doctrine2反向持久性在嵌套表单中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个与 MotorsAdsFile 实体链接的 MotorsAds 实体。对于每个 MotorsAds ,我们可以附加许多 MotorsAdsFile

I am creating a MotorsAds entity to which I am linking with MotorsAdsFile entity. For each MotorsAds, we could attach many MotorsAdsFile.

我的目标是为MotorsAds创建一个表单,只需点击按钮即可添加MotorsAdsFile。在这里,我正在尝试实施嵌入表单

My objective is creating a form for MotorsAds which allows adding MotorsAdsFile just through a click on a button. Here, I am trying to implement the embeded forms.

我的问题是我收到了错误:

My problem is that I got that error:

执行'INSERT INTO MotorsAdsFile(filename, motors_id)VALUES(?,?)'with params [5493b613839d7_2012-07-02 22.06.00.jpg,null]:

SQLSTATE [23000]:完整性约束违规:1048列'motors_id'不能为空

可能是MotorsAds对象尚未持久化:这就是为什么列'motors_id'为空。你可以在这个问题上帮忙吗?

Probably, the MotorsAds object is not persisted yet: that's why the Column 'motors_id' is null. Could you help in this issue?

我错过了什么吗?

<?php

namespace Minn\AdsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @Vich\Uploadable
 */
class MotorsAdsFile {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Assert\File(
     *     maxSize="5M",
     *     mimeTypes={"image/png", "image/jpeg"}
     * )
     * @Vich\UploadableField(mapping="motors_files", fileNameProperty="filename")
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, name="filename")
     * @var string $filename
     */
    protected $filename;

    /**
     * @ORM\ManyToOne(targetEntity="Minn\AdsBundle\Entity\MotorsAds", inversedBy="files")
     * @ORM\JoinColumn(nullable=false,onDelete="CASCADE")
     */
    private $motors;

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
     */
    public function setFile(File $file) {
        $this->file = $file;
    }

    /**
     * @return File
     */
    public function getFile() {
        return $this->file;
    }

    /**
     * @param string $filename
     */
    public function setFilename($filename) {
        $this->filename = $filename;
    }

    /**
     * @return string
     */
    public function getFilename() {
        return $this->filename;
    }

    /**
     * Set motors
     *
     * @param \Minn\AdsBundle\Entity\MotorsAds $motors
     * @return MotorsAds
     */
    public function setMotors(\Minn\AdsBundle\Entity\MotorsAds $motors) {
        $this->motors = $motors;
        return $this;
    }

    /**
     * Get motors
     *
     * @return \Minn\AdsBundle\Entity\MotorsAds
     */
    public function getMotors() {
        return $this->motors;
    }
}



MotorsAds



MotorsAds

<?php

namespace Minn\AdsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * MotorsAds
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Minn\AdsBundle\Entity\MotorsAdsRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class MotorsAds {
    // ...
    /**
     * @ORM\OneToMany(targetEntity="Minn\AdsBundle\Entity\MotorsAdsFile",
     *                mappedBy="motors",  
     *                cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $files;
    public function __construct() {
        $this->files = new \Doctrine\Common\Collections\ArrayCollection();
        $this->favorites = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getFiles() {
        return $this->files;
    }

    public function addFile(MotorsAdsFile $file) {
        $this->files[] = $file;
        return $this;
    }

    public function removeFile(MotorsAdsFile $file) {
        $this->files->removeElement($file);
    }
    // ...
}



2。表单



MotorsAdsFileType



2. The forms

MotorsAdsFileType

class MotorsAdsFileType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('file', 'file',array('required' => false));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Minn\AdsBundle\Entity\MotorsAdsFile'
        ));
    }

    public function getName() {
        return 'MotorsAdsFiletype';
    }

}



MotorsAdsType



MotorsAdsType

class MotorsAdsType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        // ...some code here
        $builder->add('files','collection',array('type'=> new MotorsAdsFileType(),
                        'allow_add'=> true,
                        'allow_delete' => true,
                        'by_reference' => true));
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Minn\AdsBundle\Entity\MotorsAds'
        ));
    }

    public function getName() {
        return 'MotorsAdstype';
    }
}

推荐答案

我找到解决方案...

I found the solution...

需要做两件事:

(1)

public function buildForm(FormBuilderInterface $builder, array $options) {
    // ...some code here
    $builder->add('files','collection',array('type'=> new MotorsAdsFileType(),
                    'allow_add'=> true,
                    'allow_delete' => true,
                    'by_reference' => false)); // it has to be set to false and not true
}

2)

public function addFile(MotorsAdsFile $file) {
    $file->setMotors($this);      // you have to add this line too
    $this->files[] = $file;
    return $this;
}

这篇关于Doctrine2反向持久性在嵌套表单中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆