我正在使用Zend Framework 3编写应用程序。为了管理数据库,我决定使用Doctrine。我有两个表页面和pages_meta(基于wordpress db的东西)。它们以一对多,多对一的关系相互融合。在pages_meta中,我有关键的page_id。现在,当我尝试获取元表单页面实体时,我得到了以下错误:
文件:/home/platne/serwer18346/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:169 消息:在null
上调用成员函数setValue()
现在申请代码:
页面实体(删除了一些代码以显示重要部分):
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实体(这里我删除了一些代码):
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;
}
}
在控制器中:
$this->getEntityManager()->getRepository(Page::class);
$page = $pagesTable->findOneBySlug($slug);
//check if page exists
$page->getPageMeta('test'); //this line cause the problem.
您可以在页面上看到完整堆栈错误:http://bibliotekadomowa.pl/o-nas
答案 0 :(得分:3)
我认为这可能是" mappedBy"页面中的参数,尝试将其更改为
mappedBy="page"
因为它应该匹配变量名而不是表名