I'm writing an application using Zend Framework 3. To manage database I decided to use Doctrine. I have two tables pages and pages_meta(something based on wordpress db). They are realted to each other in one-to-many, many-to-one relation. In pages_meta I have key page_id. Now when I try to get meta form Page Entity I got following error:
File: /home/platne/serwer18346/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:169 Message: Call to a member function setValue() on null
Now the application code:
Page Entity(removed some code to show important part):
namespace Core\Model;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Page
* @package Core\Model
* @ORM\Entity
* @ORM\Table(name="pages")
*/
class Page
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id")
*/
protected $id;
//other fields definition(here is slug to found by)
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="\Core\Model\PageMeta", mappedBy="pages")
* @ORM\JoinColumn(name="id", referencedColumnName="page_id")
*/
protected $meta;
/**
* Page constructor.
*/
public function __construct()
{
$this->meta = new ArrayCollection();
}
/**
* @return int
*
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $key
* @return ArrayCollection
*/
public function getPageMeta($key = null){
if(!$key) return $this->meta;
return $this->meta->current(); //this is causing the problem tried other functions as well
}
}
PageMeta Entity(same here I removed some code):
namespace Core\Model;
use Doctrine\ORM\Mapping as ORM;
/**
* Class PageMeta
* @package Core\Model
* @ORM\Entity
* @ORM\Table(name="page_meta")
*/
class PageMeta
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id")
*/
protected $id;
/**
* @var int
* @ORM\Column(type="integer", name="page_id")
*/
protected $page_id;
/**
* @var Page
* @ORM\ManyToOne(targetEntity="\Core\Model\Page", inversedBy="page_meta")
* @ORM\JoinColumn(name="page_id", referencedColumnName="id")
*/
protected $page;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return int
*/
public function getPageId(): int
{
return $this->page_id;
}
/**
* @param int $page_id
* @return PageMeta
*/
public function setPageId(int $page_id): PageMeta
{
$this->page_id = $page_id;
return $this;
}
//other field definition
/**
* @return Page
*/
public function getPage(){ //this works fine
return $this->page;
}
}
In the controller:
$this->getEntityManager()->getRepository(Page::class);
$page = $pagesTable->findOneBySlug($slug);
//check if page exists
$page->getPageMeta('test'); //this line cause the problem.
Full stack error you can see on page: http://bibliotekadomowa.pl/o-nas