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

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

你好,我正在尝试通过此 link https://www.tutorialspoint.com/symfony/symfony_complete_working_example.htm

完成本教程

我已完成第 15 步:收集图书信息并存储

当我尝试从 newAction 表单输入时,我收到了错误消息

Warning: count(): Parameter must be an array or an object that implements 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