Symfony2 Embed form on multiple entities

可紊 提交于 2020-01-16 18:48:47

问题


I have 3 entities User

class User extends BaseUser
{    
    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="user")
     */ 
    protected $userPathologies;
}

Pathologie

class Pathologie
{
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    private $nom;

    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="pathologie")
     */ 
    protected $userPathologies;
}

UserPathologie

class UserPathologie
{
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="userPathologies")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Pathologie", inversedBy="userPathologies")
     */ 
    protected $pathologie;

     /**
     * @var boolean
     *
     * @ORM\Column(name="atteint", type="boolean")
     */
    private $atteint;

    /**
     * @var string
     *
     * @ORM\Column(name="cause", type="text")
     */
    private $cause;

}

Then, on the User form, I want to have the list of all "Pathologies" with the checkbox "atteint" and textarea "cause".

ex:

--label-- Pathologies --/label--

Pathologie A : Yes/No - Textarea cause

Pathologie B : Yes/No - Textarea cause

Pathologie C : Yes/No - Textarea cause

Pathologie D : Yes/No - Textarea cause

Pathologie E : Yes/No - Textarea cause

I proceed like below but the inconvenient is that each line should be added dynamically with javascript and the "pathologies" are in a select field.

in UserRegiterType

public function buildForm(FormBuilderInterface $builder, array $options)
{   

    $builder
        ->add('userPathologies', 'collection', array(
            'type' => new UserPathologieType(), 
            'label' => false, 
            'allow_add' => true,
        ));
}

And in UserPathologieType, I have

public function buildForm(FormBuilderInterface $builder, array $options)
{   

    $builder        
        ->add('pathologie')
        ->add('atteint', 'checkbox', array(
            'label' => false,
            'required' => false, 
        ))
        ->add('cause', 'textarea', array(
            'label' => 'Cause', 
            'required' => false, 
        ));
}

回答1:


To avoid patologies in select field override the widget for the field

{% block pathologie_widget %}
    <b>{{ value }}</b>
{% endblock %}

See http://symfony.com/doc/current/book/forms.html#form-theming for details about for theming

Or depending on how you are rendering your collection you can also take this way: Symfony2 formbuilder - entity readonly field as label

To have all pathologies connected to a new user just add them in the __construct of User

public function __construct() {

   // get all phatologies

   foreach ($allPhatologies as UserPathologie) {
       $this->userPathologies->add(new UserPathologie());
   }

}



回答2:


This is how I did to get it work

First add cascade={"persist"} to the OneToMany relation in User to persist automatically the user pathologies

Class User
{
    /**
     * @ORM\OneToMany(targetEntity="Myapp\UserBundle\Entity\UserPathologie", mappedBy="user", cascade={"persist"})
     */ 
    protected $userPathologies;
}

Then, add all pathologies to the new entity as @Hpatoio suggested

private function createRegisterForm(User $entity)
{
    $em = $this->getDoctrine()->getManager();
    $pathologies =  $em->getRepository("MyappUserBundle:Pathologie")->findAll();

    //Loop all pathologies
    foreach($pathologies as $patho) {
        $up = new UserPathologie();
        $up->setPathologie($patho);
        $up->setUser($entity);
        $up->setAtteint(false);

        //Add each pathologie to the User entity
        $entity->getUserPathologies()->add($up);
    }

    $form = $this->createForm(new UserRegisterType(), $entity, array(
        'action' => $this->generateUrl('register_user'),
        'method' => 'POST',
    ));

    return $form;
}

And in the twig template, I used again a loop to get the expected result

{% for up in form.userPathologies%}
    <input type="hidden" name="{{ up.pathologie.vars.full_name }}" id="{{ up.pathologie.vars.id }}" value="{{ up.pathologie.vars.value }}" />
    <p class="line"><label>{{ up.vars.value.pathologie }}</label>{{ form_widget(up.atteint) }}</p>
    <p class="line">{{ form_row(up.cause) }}</p>
{% endfor %}


来源:https://stackoverflow.com/questions/26137298/symfony2-embed-form-on-multiple-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!