简体   繁体   中英

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

"model" has a ManyToOne relationship with "brand"

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. I changed the encoding to utf8, clear the cache - and the error doesn't occur again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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