Symfony表单:如何保存相关实体的属性?

时间:2016-05-19 13:42:09

标签: symfony-forms symfony

我是Symfony的新手,对形式和关系有非常基本的问题。

目标是基于包含与另一实体相关的属性的实体来构建用于保存/编辑对象的表单。遗憾的是,我不知道如何在buildForm方法中定义字段来保存/编辑这个相关实体的属性。

实体\ Artist.php

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="artist")
 * @UniqueEntity("name")
 */
class Artist {

    const THEMES_ENABLED = true;

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

    /**
     * @ORM\Column(type="string", length=150, unique=true, nullable=false)
     * @Assert\NotBlank()
     */
    private $name;


    /**
     * @ORM\OneToOne(targetEntity="ArtistBiography", mappedBy="artist")
     */
    private $artistBiography;

    // Getter & Setter ...        
}

实体\ ArtistBiography.php

**
 * @ORM\Entity
 * @ORM\Table(name="artist_biography")
 */
class ArtistBiography {

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

    /**
     * @ORM\OneToOne(targetEntity="Artist", inversedBy="artistBiography")
     * @ORM\JoinColumn(name="artist_id", referencedColumnName="id")
     */
    private $artist;

    /**
     * @ORM\Column(type="string", length=150, unique=false, nullable=false)
     */
    private $headline;

    /**
     * @ORM\Column(type="string", length=150, nullable=false)
     */
    private $text;    

    // Getter & Setter ...

}

我使用此自定义FormType进行渲染:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
//use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ArtistType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('name', TextType::class)
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Entity\Artist',
        ));
    }
}

如何为属性' artistBiography.headline' (TextType)和' artistBiography.text' 添加字段(TextareaType)?

0 个答案:

没有答案