画像をアップロードしましたが、次の 2 つのことが起こることに気付きました。
1) フォームは更新時に再送信されます。明らかにそれを望んでいません。フラットなPHPの答えだけを見つけました。私はそれを行うsymfonyの方法は何だろうと思っていました.
2) ファイルをアップロードした後、その画像を表示するには更新する必要があります。これが問題 1 に気付いた方法です。
コントローラーコード:
public function displayThreadAction($thread_Id)
{
$em = $this->getDoctrine()->getManager();
$thread = $em->getRepository('GreenMonkeyDevGlassShopBundle:ForumThread')->find($thread_Id);
$post = new ForumReply();
$post->setThreadId($thread);
$form = $this->createForm(new ReplyImageForm(), $post);
$request = $this->getRequest();
if ($request->isMethod('POST')){
$form->bind($request);
if ($form->isValid()){
$image = new ForumReplyImage();
$image->setImageName($form['imageName']->getData());
$image->setImageFile($form['imageFile']->getData());
$image->upload();
$image->setReplyId($post);
$em->persist($post);
$em->persist($image);
$em->flush();
$post = new ForumReply();
$post->setThreadId($thread);
$form = $this->createForm(new ReplyImageForm(), $post);
}
}
return $this->render('GreenMonkeyDevGlassShopBundle:Forum:forum_thread.html.twig', array('thread' => $thread, 'form' => $form->createView()));