Symfony 3.3 ContextErrorException

时间:2017-06-10 07:37:43

标签: php symfony doctrine symfony-forms

我有两个实体如下:

    <?php
// src/coreBundle/Entity/model.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\brand;

/**
*@ORM\Entity
*@ORM\Table(name="model")
*/
class model
{
    /**
    * @ORM\ManyToOne(targetEntity="brand", inversedBy="models")
    * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
    */
    private $brands;

第二个如下:

    <?php
// src/coreBundle/Entity/brand.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\model;
use Doctrine\Common\Collections\ArrayCollection;

/**
*@ORM\Entity
*@ORM\Table(name="brand")
*/
class brand
{
    /**
     * ORM\OneToMany(targetEntity="model", mappedBy="brands")
     */
    private $models;
    public function __construct()
    {
        $this->models = new ArrayCollection();
    }

现在在我的控制器中,我有以下代码

public function createBrandFormAction()
    {
        $brand = new brand();
        $form = $this->createFormBuilder($brand)
        ->add('name',TextType::class,array('label'=>'Brand Name'))
        ->add('save',SubmitType::class, array('label'=>'Add Brand'))
        ->getForm();

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
        // perform some action, such as saving the task to the database

            $em = $this->getDoctrine()->getManager();
            $brand = $form->getData();
            $em->persist($brand);
            $em->flush();
            return $this->render('coreBundle:layouts:newBrand.html.twig',
                array('form'=>$form->createView(),));
        }

        return $this->render('coreBundle:layouts:newBrand.html.twig',
            array('form'=>$form->createView(),));
    }

执行控制器时,我遇到了异常

  

捕获致命错误:传递给Symfony \ Component \ HttpFoundation \ Session \ Attribute \ AttributeBag :: initialize()的参数1必须是类型数组,给定对象,在C:\ ledger \ var \ cache \ dev \中调用第248行的classes.php并定义了

&#34;模型&#34;与&#34;品牌&#34;

建立了ManyToOne关系

你能说出我做错了什么,提前谢谢。

Stack Trace如下:

ContextErrorException
Symfony\Component\Debug\Exception\ContextErrorException:
Catchable Fatal Error: Argument 1 passed to Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::initialize() must be of the type array, object given, called in C:\ledger\var\cache\dev\classes.php on line 248 and defined

  at vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag.php:57
  at Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag->initialize(object(Stub))
     (var\cache\dev\classes.php:248)
  at Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->loadSession()
     (var\cache\dev\classes.php:116)
  at Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start()
     (var\cache\dev\classes.php:192)
  at Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->getBag('attributes')
     (var\cache\dev\classes.php:540)
  at Symfony\Component\HttpFoundation\Session\Session->getAttributeBag()
     (var\cache\dev\classes.php:459)
  at Symfony\Component\HttpFoundation\Session\Session->get('_security_main')
     (vendor\symfony\symfony\src\Symfony\Component\Security\Http\Firewall\ContextListener.php:83)
  at Symfony\Component\Security\Http\Firewall\ContextListener->handle(object(GetResponseEvent))
     (var\cache\dev\classes.php:4700)
  at Symfony\Component\Security\Http\Firewall->onKernelRequest(object(GetResponseEvent))
     (vendor\symfony\symfony\src\Symfony\Bundle\SecurityBundle\EventListener\FirewallListener.php:48)
  at Symfony\Bundle\SecurityBundle\EventListener\FirewallListener->onKernelRequest(object(GetResponseEvent))
     (var\cache\dev\appDevDebugProjectContainer.php:783)
  at appDevDebugProjectContainer->{closure}(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
  at call_user_func(object(Closure), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php:112)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher))
  at call_user_func(object(WrappedListener), object(GetResponseEvent), 'kernel.request', object(ContainerAwareEventDispatcher))
     (var\cache\dev\classes.php:3429)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent))
     (var\cache\dev\classes.php:3344)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher.php:146)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (var\cache\dev\classes.php:4380)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (var\cache\dev\classes.php:4350)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:171)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web\app_dev.php:30)
  at require('C:\\ledger\\web\\app_dev.php')
     (vendor\symfony\symfony\src\Symfony\Bundle\WebServerBundle\Resources\router.php:42)

1 个答案:

答案 0 :(得分:0)

可能另一个原因导致可捕获的致命错误:我遇到了同样的问题,并且发现控制器不是utf8编码的。我将编码更改为utf8,清除缓存 - 并且错误不会再次发生。