Symfony2のエンティティの1つにCRUDがあります。新しいエントリを作成するために、2つのコントローラー機能があります。
public function newAction($id) {
$entity = new Clientes();
// Get the reference to the Login entity using its ID
$em = $this->getDoctrine()->getManager();
$ref_login = $em->getReference('LoginBundle:Login', $id);
// Put the retrieved reference to the entity
$entity->setLogin($ref_login);
$form = $this->createForm(new ClientesType(), $entity);
return $this
->render('MovinivelBundle:Persona/Clientes:new.html.twig',
array('entity' => $entity,
'form' => $form->createView(),));
}
public function createAction(Request $request) {
$entity = new Clientes();
$form = $this->createForm(new ClientesType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('clientes'));
}
return $this
->render('MovinivelBundle:Persona/Clientes:new.html.twig',
array('entity' => $entity,
'form' => $form->createView(),));
}
前のコードでは、$ id入力パラメーターをnewAction()関数に追加しました。これは、このクライアントのそれぞれがログインの追加情報であり、リンクする必要があるため、外部から確立する必要があるためです。
ClientesTypeフォームには、次のものがあります。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('login')
->add('direccion')
->add('localidad')
->add('provincia')
->add('telefono')
;
}
これまでのところ、それは機能します。私のフォームでは、ログインパラメータは$id値に応じて選択されます。ただし、フォームの作成後にログインパラメータを修正したいので、ユーザーはフォームからログインパラメータを変更できず、適切な値を使用してnewAction($ id)関数を呼び出すだけです。
FormTypeの->add('login')行を削除すると、機能しなくなります。私の頭に浮かぶのは2つの選択肢です。
- どういうわけかフォームの「ログイン」を非表示にしますが、方法はわかりませんが、機能し続けます。
- $idパラメーターと$requestパラメーターを入力パラメーターとしてcreateActionに渡しますが、その方法もわかりません。
これについて何か考えはありますか?