我有两个实体'状态'和'医生'。每个医生都可以在他的个人资料中添加状态。当医生想要添加状态时,他可以为另一位医生添加状态,因为选择字段。
这不是逻辑,我希望每个医生只添加他的身份,而不是其他医生。
我该如何解决?
这是状态表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('medecin','entity',array('class'=>'DoctorBundle:medecin','property'=>'prenom','multiple'=>false))
->add('text')
->add('description')
->add('image',new ImageType())
;
}`
从表单中删除字段医生(为您使用医生),然后在处理表单响应时在控制器中删除
字段 if ('POST' === $request->getMethod()) {
$statusForm->handleRequest($request);
if ($statusForm->isValid()) {
$status = $statusForm->getData();
$status->setDoctor($this->getUser());
$statusManager->flush($status);
}
}
$this->getUser() 如果你是用医生登录的,如果你不是,请随便找他。
好的,根据您的评论,我认为您有几种方法可以解决此问题:
-
完全删除
medecin
表单域,稍后再分配。例如:$status = ....; // Your entity $currentDoctor = ... // logic for getting myself :) $status->setMedecin($currentDoctor); $form = $this->createForm(StatusType(), $status); $form->handleRequest($request); if ( $form->isValid() ){ // The rest is the same }
-
绑定
medecin
表单域data
属性。这进一步使事情复杂化,因为您需要将自己(currentDoctor
)传递到表单类型中。我肯定会选择上面的#1。
希望这有帮助...
我在创建表单的状态控制器中有以下代码:
/**
* Creates a form to create a statut entity.
*
* @param statut $entity The entity
*
* @return 'Symfony'Component'Form'Form The form
*/
private function createCreateForm(statut $entity)
{
$form = $this->createForm(new statutType(), $entity, array(
'action' => $this->generateUrl('statut_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
'
我尝试使用此代码,它可以使用选择字段,但 Doctor-id 取值 null,这是代码创建表单:$id= $entity->setMedecin();
$form = $this->createForm(new statutType(), $entity, array(
'action' => $this->generateUrl('statut_create'),
'method' => 'POST',
));