Php Symfony2在多个实体上嵌入表单

Php Symfony2在多个实体上嵌入表单,php,mysql,symfony,doctrine-orm,symfony-2.5,Php,Mysql,Symfony,Doctrine Orm,Symfony 2.5,我有3个实体 用户 class User extends BaseUser { /** * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="user") */ protected $userPathologies; } 病理学 class Pathologie { /** * @var string * * @ORM\Column(name="no

我有3个实体 用户

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

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

    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="pathologie")
     */ 
    protected $userPathologies;
}
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;

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

    $builder
        ->add('userPathologies', 'collection', array(
            'type' => new UserPathologieType(), 
            'label' => false, 
            'allow_add' => true,
        ));
}
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, 
        ));
}
用户病理学

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

    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="pathologie")
     */ 
    protected $userPathologies;
}
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;

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

    $builder
        ->add('userPathologies', 'collection', array(
            'type' => new UserPathologieType(), 
            'label' => false, 
            'allow_add' => true,
        ));
}
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, 
        ));
}
然后,在用户表单上,我想要所有“病理学”的列表,带有复选框“atteint”和文本区域“cause”

例:

--标签--病理学--/label--

病理学A:是/否-text区域原因

病理学B:是/否-text区域原因

病理学C:是/否-text区域原因

病理学D:是/否-text区域原因

病理学E:是/否-text区域原因

我继续如下操作,但不方便的是,每一行都应该使用javascript动态添加,“pathologies”在一个select字段中

在UserRegiterType中

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

    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="pathologie")
     */ 
    protected $userPathologies;
}
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;

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

    $builder
        ->add('userPathologies', 'collection', array(
            'type' => new UserPathologieType(), 
            'label' => false, 
            'allow_add' => true,
        ));
}
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, 
        ));
}
在UserPathologieType中,我有

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

    /**
     * @ORM\OneToMany(targetEntity="UserPathologie", mappedBy="pathologie")
     */ 
    protected $userPathologies;
}
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;

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

    $builder
        ->add('userPathologies', 'collection', array(
            'type' => new UserPathologieType(), 
            'label' => false, 
            'allow_add' => true,
        ));
}
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, 
        ));
}

要避免选择字段中的
patologies
,请覆盖该字段的小部件

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

我就是这样让它工作的

首先将
cascade={“persist”}
添加到User中的OneToMany关系,以自动持久化用户病理

Class User
{
    /**
     * @ORM\OneToMany(targetEntity="Myapp\UserBundle\Entity\UserPathologie", mappedBy="user", cascade={"persist"})
     */ 
    protected $userPathologies;
}
然后,按照@Hpatoio的建议,将所有病理学添加到新实体中

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;
}
在twig模板中,我再次使用了一个循环来获得预期的结果

{% 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 %}
{%用于表单中的up.userPathologies%}

{{up.vars.value.pathologie}{{form_小部件(up.atteint)}

{{form_行(up.cause)}

{%endfor%}
谢谢!它起作用了!但是我没有把它放在构造函数中,而是把它放在控制器中,以便得到所有的病理学