簡體   English   中英

symfony 2.8 使用 if ($form->isSubmitted() && $form->isValid())

[英]symfony 2.8 using if ($form->isSubmitted() && $form->isValid())

你好,我正在嘗試通過此鏈接完成本教程https://www.tutorialspoint.com/symfony/symfony_complete_working_example.htm

我已經完成了第 15 步:收集圖書信息並存儲它

當我嘗試從 newAction 表單輸入時收到錯誤消息

警告:count():參數必須是一個數組或一個實現 Countable 的對象

哦,我忘了提到我使用 symfony 2.8.3

這是我的代碼

<?php
// scr/AppBundle/Controller/BooksController.php
namespace AppBundle\Controller;

use AppBundle\Entity\Book;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class BooksController extends Controller {
    /**
     * @Route("/books/author")
     */
    public function authorAction() 
    {
        return $this->render('books/author.html.twig');
    }

    /**
     * @Route("/books/display", name = "app_book_display")
     */
    public function displayAction()
    {
        $bk = $this->getDoctrine()
        ->getRepository('AppBundle:Book')
        ->findAll();

        return $this->render('books/display.html.twig', array('data' => $bk));
    }

    /**
     * @Route("/books/new", name = "app_book_new")
     */
    public function newAction(Request $request)
    {
        $book = new Book();
           $form = $this->createFormBuilder($book)
              ->add('name', TextType::class)
              ->add('author', TextType::class)
              ->add('price', TextType::class)
              ->add('save', SubmitType::class, array('label' => 'Submit'))
              ->getForm();

           $form->handleRequest($request);

           if ($form->isSubmitted() && $form->isValid()) {
               $book = $form->getData();
               $doct = $this->getDoctrine()->getManager();

               // tells Doctrine you want to save the Product
               $doct->persist($book);

               // executes the queries (i.e. the INSERT query)
               $doct->flush();

               return $this->redirectToRoute('app_book_display');
           } else {
            return $this->render('books/new.html.twig', array('form' => $form->createView(),
        ));
        }
    }

    /**
     * @Route("/books/update/{id}", name = "app_book_update")
     */
    public function updateAction($id, Request $request)
    {
        $doct = $this->getDoctrine()->getManager();
            $bk = $doct->getRepository('AppBundle:Book')->find($id);

            if (!$bk) {
                throw $this->createNotFoundException(
                    'No book found for id '.$id
                );
            }
            $form = $this->createFormBuilder($bk)
                ->add('name', TextType::class)
                ->add('author', TextType::class)
                ->add('price', TextType::class)
                ->add('save', SubmitType::class, array('label' => 'Submit'))
                ->getForm();

            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $book = $form->getData();
                $doct = $this->getDoctrine()->getManager();

                // tells Doctrine you want to save the Product
                $doct->persist($book);

                // executes the queries (i.e. the INSERT query)
                $doct->flush();

                return $this->redirectToRoute('app_book_display');
            } else {
                return $this->render('books/new.html.twig', array(
                    'form' => $form->createView(),
                ));
            }
    }

    /**
     * @Route("/books/delete/{id}", name = "app_book_delete")
     */
    public function deleteAction($id)
    {
        $doct = $this->getDoctrine()->getManager();
        $bk = $doct->getRepository('AppBundle:Book')->find($id);

        if (!$bk) {
            throw $this->createNotFoundException('No Book found for id '.$id);
        }
        $doct->remove($bk);
        $doct->flush();
        return $this->redirectToRoute('app_book_display');
    }
}

我希望輸出將是來自表單的輸入 newAction 將存儲數據並重定向我以顯示並顯示來自先前輸入的數據

好的,我解決了這個問題,只是使用 Symfony 2.8.39 創建項目他們已經解決了這個問題https://symfony.com/blog/symfony-2-8-39-released

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM