Pisząc aplikację związaną z książką "Symfony 2 od podstaw", mam taki problem:
Warning: spl_object_hash() expects parameter 1 to be object, null given in D:\xampp\htdocs\Backend5\vendor\doctrine\lib\Doctrine\ORM\UnitOfWork.php line 1977
Problem pojawia się przy każdej próbie wyciągnięcia danych z bazy danych. Jest to projekt przepisany z książki, tak że powinien działać.
Wcześniej w tym projekcie dane były pobierane z bazy bez logowania, ale wszystko było w jednym pakiecie.
Logowanie działa, problem pojawia się gdy próbuję wyświetlic liste kontynentow lub państw.
Poniżej zamieszczę kod Kontrolerów i encji:
Kontynent kontroler:
<?php namespace My\FrontendBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use My\FrontendBundle\Entity\Kontynent; class KontynentController extends Controller { /** * Lista kontynentow * * @Route("/kontynenty.html", name="kontynent_index") * @Template() */ public function indexAction() { $em = $this->getDoctrine()->getEntityManager(); $entities = $em->getRepository('MyBackendBundle:Kontynent')->findAll(); } /** * Szczegolowe dane kontynentu * * @Route("/kontynent/{id}.html", name="kontynent_show") * @Template() */ public function showAction($id) { $em = $this->getDoctrine()->getEntityManager(); $entity = $em->getRepository('MyBackendBundle:Kontynent')->find($id); if (!$entity) { throw $this->createNotFoundException('Brak kontynentu o podanym id!'); } } }
Pnastwo Kontroler:
<?php namespace My\FrontendBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use My\FrontendBundle\Entity\Panstwo; class PanstwoController extends Controller { /** * Lista wszystkich panstw * * @Route("/panstwa/html", name="panstwo_index") * @Template() */ public function indexAction() { $em = $this->getDoctrine()->getEntityManager(); $entities = $em->getRepository('MyBackendBundle:Panstwo')->findAll(); } /** * Szczegolowe dane panstwa * * @Route("/panstwo/{id}.html", name="panstwo_show") * @Template() */ public function showAction($id) { $em = $this->getDoctrine()->getEntityManager(); $entity = $em->getRepository('MyBackendBundle:Panstwo')->find($id); if (!$entity) { throw $this->createNotFoundException('Brak panstwa o podanym id!'); } } }
Encja Kontynent:
<?php namespace My\BackendBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * My\BackendBundle\Entity\Kontynent * * @ORM\Table() * @ORM\Entity */ class Kontynent { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $nazwa * * @ORM\Column(name="nazwa", type="string", length=255) */ private $nazwa; /** * @ORM\OneToMany(targetEntity="Panstwo", mappedBy="kontynent") */ protected $panstwa; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nazwa * * @param string $nazwa */ public function setNazwa($nazwa) { $this->nazwa = $nazwa; } /** * Get nazwa * * @return string */ public function getNazwa() { return $this->nazwa; } /** * Get nazwa * * @return string */ public function __toString() { return (string)$this->getNazwa(); } public function __construct() { $this->panstwa = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add panstwa * * @param My\BackendBundle\Entity\Panstwo $panstwa */ public function addPanstwo(\My\BackendBundle\Entity\Panstwo $panstwa) { $this->panstwa[] = $panstwa; } /** * Get panstwa * * @return Doctrine\Common\Collections\Collection */ public function getPanstwa() { return $this->panstwa; } }
<?php namespace My\BackendBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * My\BackendBundle\Entity\Panstwo * * @ORM\Table() * @ORM\Entity */ class Panstwo { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $nazwa * * @ORM\Column(name="nazwa", type="string", length=255) */ private $nazwa; /** * @ORM\ManyToOne(targetEntity="Kontynent", inversedBy="panstwa") */ protected $kontynent; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nazwa * * @param string $nazwa */ public function setNazwa($nazwa) { $this->nazwa = $nazwa; } /** * Get nazwa * * @return string */ public function getNazwa() { return $this->nazwa; } /** * Get nazwa * * @return string */ public function __toString() { return $this->getNazwa(); } /** * Set kontynent * * @param My\BackendBundle\Entity\Kontynent $kontynent */ public function setKontynent(\My\BackendBundle\Entity\Kontynent $kontynent) { $this->kontynent = $kontynent; } /** * Get kontynent * * @return My\BackendBundle\Entity\Kontynent */ public function getKontynent() { return $this->kontynent; } }
Projekt jest podzielony na 3 pakiety, FrontendBundle, BackendBundle i UserBundle.
W jednym z nich są kontrolery wygenerowane dla Panstwa i kontynentu przez panel CRUD.
Więc w aplikacji są po dwa kontrolery dla państwa i Kontynentu ale w różnych pakietach.