【问题标题】:symfony 2.8 using if ($form->isSubmitted() && $form->isValid())symfony 2.8 使用 if ($form->isSubmitted() && $form->isValid())
【发布时间】:2019-01-30 10:26:04
【问题描述】:

您好,我正在尝试通过此链接 https://www.tutorialspoint.com/symfony/symfony_complete_working_example.htm 完成本教程

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

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

警告:count():参数必须是数组或对象 实现可数

哦,我忘了提到我使用 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 将存储数据并将我重定向到显示并显示来自先前输入的数据

【问题讨论】:

  • 我在你的代码中没有看到任何地方实现count()
  • @catcon 我知道但这里是图片错误输出pasteboard.co/HYPj9ZW.png

标签: php symfony-2.8


【解决方案1】:

好的,我解决了这个问题,只需使用 Symfony 2.8.39 创建项目,他们已经解决了这个问题https://symfony.com/blog/symfony-2-8-39-released

【讨论】:

  • 确认。 php7.2,我不得不从SF2.8.9升级到2.8.39,这个错误消失了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多