我有3个实体 用户
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;
}
然后,在用户表单上,我想要列出所有" Pathologies"使用复选框"参加"和textarea"原因"。
例如:
- 标签 - 病理 - /标签 -
病理A:是/否 - Textarea原因
病理学B:是/否 - Textarea原因
Pathologie C:是/否 - Textarea原因
病理学D:是/否 - Textarea原因
Pathologie E:是/否 - Textarea原因
我继续如下,但不方便的是,应该使用javascript和"病态"动态添加每一行。在选择字段中。
UserRegiterType中的
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('userPathologies', 'collection', array(
'type' => new UserPathologieType(),
'label' => false,
'allow_add' => true,
));
}
在UserPathologieType中,我有
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,
));
}
答案 0 :(得分:2)
要避免选择字段中的patologies
覆盖字段的小部件
{% block pathologie_widget %}
<b>{{ value }}</b>
{% endblock %}
有关主题的详细信息,请参阅http://symfony.com/doc/current/book/forms.html#form-theming
或者,根据您渲染集合的方式,您也可以采用以下方式: Symfony2 formbuilder - entity readonly field as label
要让所有与新用户相关的病症都将其添加到__construct
User
public function __construct() {
// get all phatologies
foreach ($allPhatologies as UserPathologie) {
$this->userPathologies->add(new UserPathologie());
}
}
答案 1 :(得分:0)
这就是我如何让它发挥作用
首先将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;
}
在树枝模板中,我再次使用循环来获得预期结果
{% 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 %}