loicb
loicb

Reputation: 645

Symfony OneToOne relation render unique form

I have an entity with too many fields and datas to be handled by MySQL.

So I made another entity to store contents and linked it to the parent entity with OneToOne relations.

Here an extract of my parent entity HomeContent

// ...
class HomeContent
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="locale", type="string", length=6)
 */
private $locale;

/**
 * @var string
 *
 * @ORM\OneToOne(targetEntity="ContentBlock", cascade={"all"})
 */
private $healthIntro;

/**
 * @var string
 *
 * @ORM\OneToOne(targetEntity="ContentBlock", cascade={"all"})
 */
private $desktopIntro;

/**
 * @var string
 *
 * @ORM\OneToOne(targetEntity="ContentBlock", cascade={"all"})
 */
private $techIntro;

// ...

public function __construct()
{
    $this->healthIntro = new ContentBlock();
    $this->desktopIntro = new ContentBlock();
    $this->techIntro = new ContentBlock();
// ...

My ContentBlockentity has one text field content with setter and getter.

Now I want to simply render my form with a textarea for each fields, what would be the best way to do it?

For now, they're rendered as select elements, I defined a ContentBlockType class with content field

// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content', 'textarea');
}
// ...

And a HomeContentType of course

// ...
 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text')
        ->add('metadescription', 'text')
        ->add('healthIntro', 'entity', array(
            'class' => 'NavaillesMainBundle:ContentBlock'
        ))
// ...

Upvotes: 1

Views: 245

Answers (1)

staskrak
staskrak

Reputation: 863

First of all I suggest to keep with a rule to use JoinColumn()annotation. Example:

/**
 * @var string
 *
 * @ORM\OneToOne(targetEntity="ContentBlock", cascade={"all"})
 * @ORM\JoinColumn(name="desktop_intro_id", referencedColumnName="id")
 */
private $desktopIntro;

Answer:

I don't know whether my way is the best but I suggest that you create ContentBlockFormType and embedded it to your form. So the form of your HomeContent entity will be like this:

    // ...

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder
            ->add('title', 'text')
            ->add('metadescription', 'text')
            ->add('desktopIntro', ContentBlockFormType::class, [
                 'label'                 => 'Desktop intro',
                 'required'              => false,
            ])
            ->add('healthIntro', ContentBlockFormType::class, [
                 'label'                 => 'Health intro',
                 'required'              => false,
            ])
            ->add('techIntro', ContentBlockFormType::class, [
                 'label'                 => 'Tech intro',
                 'required'              => false,
            ])
    }

    // ... 

Upvotes: 1

Related Questions