对象Article
有
/**
* @var Collection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Photo", mappedBy="article", cascade={"persist"})
*/
private $photos;
Photo
其侧面的物体有
/**
* @var Article
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Article", inversedBy="photos")
*/
private $article;
我有一个Article
对象Photo
的形式,其中包含对象的形式如下:
//article form building
->add('photos', CollectionType::class, [
'allow_delete' => true,
'allow_add' => true,
'entry_type' => PhotoType::class,
'entry_options' => [
'label' => false,
]
])
在我PhotoType
我有
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image', ImageType::class, [
'context' => 'photo',
])
->add('description')
->add('url')
...
但是如何article
在Photo
表单中设置字段?这样我就可以使用article_id
表中定义的列创建照片?目前此表单中所有创建的照片的 article_id 为 NULL,换句话说,他们没有设置
在您的加法器中,您需要设置 ID:
public function addPhoto(Photo $photo)
{
if (!$this->photos->contains($photo)) {
$this->photos[] = $photo;
$photo->setDocument($this);
}
return $this;
}
在您的表单中,您需要设置:'by_reference' => false
,https : //symfony.com/doc/current/reference/forms/types/collection.html#by-reference
类似地,如果您使用 CollectionType 字段,其中您的底层集合数据是一个对象(如 Doctrine 的 ArrayCollection),那么如果您需要加法器和移除器(例如 addAuthor() 和 removeAuthor()),则必须将 by_reference 设置为 false叫做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句