これらのボードで長い時間を経て、私は最終的に私の最初の投稿をすることにしました。私は最近、Symfony(2.4、私に叫ばないでください:)で遊んでいます。 doctrineのターミナルコマンドを使用して、私はCRUDイベントを生成しました。さて、これはすばらしいことですが、URLにIDを渡す必要があることを除けば、たとえば:www.mydomain.com/account/16/。これにより、mysqlのidが16の行のデータがフォームにあらかじめ入力されます。私の質問は、URLにidを渡す必要がないように、あらかじめ作成されたCRUD(更新のみに興味があります)をどのように操作すればよいのですか?むしろ、ログインしたユーザーのIDに基づいてフォームをレンダリングしますアカウントに関連付けられていますか?ここでHow to - Symfony 2 IDをURLに渡さずにCRUDを実行する
は私のコードです:
class AccountController extends Controller
{
/**
* Displays a form to edit an existing Event entity.
* @Route("/account/{id}", name="post_login_account_edit")
* @PreAuthorize("isFullyAuthenticated()")
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('UserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
return $this->render('UserBundle:Account:account.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView()
));
}
/**
* Creates a form to edit a Event entity.
*
* @param User $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(User $entity)
{
$form = $this->createForm(new UserType(), $entity, array(
'action' => $this->generateUrl('post_login_account_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing User entity.
* @Route("/account/{id}/update", name="post_login_account_update")
* @PreAuthorize("isFullyAuthenticated()")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('UserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('post_login_account'));
}
return $this->render('UserBundle:Account:account.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView()
));
}
}
変更ルートアドレス '* @関数宣言から$ idを削除します。 '$ function = $ em-> getRepository( 'UserBundle')という行を変更して、 :User ') - > find($ id); 'to $エンティティ= $ this-> getUser(); 'IDでユーザの代わりに現在ログインしているユーザをフェッチする – JimL
これはうまくいきました!!!ありがとうございました! –
それは少なくともいくつかの意味でも作ってほしい – JimL