du22399 2017-07-13 21:10
浏览 38
已采纳

Doctrine ArrayCollection在null上调用成员函数setValue()

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

  • 写回答

1条回答 默认 最新

  • duancuan6466 2017-07-13 21:24
    关注

    I think it may be an issue with the "mappedBy" param in Page, try changing that to

    mappedBy="page"
    

    As it should match the variable name not the table name

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable