简体   繁体   English

Symfony 3.3 ContextErrorException

[英]Symfony 3.3 ContextErrorException

I have two entities as follows: 我有两个实体如下:

    <?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;

And Second one is as follows: 第二个如下:

    <?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();
    }

Now In my controller, I have following code 现在在我的控制器中,我有以下代码

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(),));
    }

While executing the controller, I am getting following Exception 在执行控制器时,我得到了以下异常

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

"model" has a ManyToOne relationship with "brand" “模特”与“品牌”有ManyToOne关系

Can you tell what am I doing wrong, Thanks in advance. 你能说出我做错了什么,先谢谢你。

Stack Trace is given below: 堆栈跟踪如下:

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)

Possible that another reason causes the Catchable Fatal Error: I have the same problem, and find out, that a controller was not utf8-encoded. 可能是另一个原因导致可捕获的致命错误:我有同样的问题,并发现控制器不是utf8编码的。 I changed the encoding to utf8, clear the cache - and the error doesn't occur again. 我将编码更改为utf8,清除缓存 - 并且错误不会再次发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM