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

时间:2019-07-20 05:34:22

标签: doctrine zend-framework3

我正在使用Zend Framework 3编写应用程序。为了管理数据库,我决定使用Doctrine。

我对《教义》有一个奇怪的问题。我已经建立了以下结构:

namespace Hotels\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as orm;

/**
* @orm\Entity
* @orm\Table(name="hotels")
*/

class Hotels
{

    /**
    * @orm\Id
    * @orm\Column(type="integer")
    * @orm\GeneratedValue
    */
    private $id;

    /**
    * One hotel has many features. This is the inverse side.
    * @var ArrayCollection
    * @orm\OneToMany(targetEntity="Photos", mappedBy="hotels")
    * @orm\JoinColumn(name="id", referencedColumnName="hotel_id")
    */
    private $photos;


    public function __construct() {
        $this->photos = new ArrayCollection();
    }

    public function getPhotos()
    {
        return $this->photos;
    }

    public function setPhotos($photos)
    {
        $this->photos = $photos;
    }

    /** @orm\Column(type="string", name="name") */
    private $name;

    /**
    * @return mixed
    */
    public function getName()
    {
        return $this->name;
    }

    /**
    * @param mixed $name
    */
    public function setName($name)
    {
       $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getId()
    {       
        return $this->id;
    }

    /**
    * @param mixed $id
    */
    public function setId($id)
    {
        $this->id = $id;
    }
}

并且:

namespace Hotels\Entity;

use Doctrine\ORM\Mapping as orm;

/**
 * @orm\Entity
 * @orm\Table(name="photos")
 */

class Photos
{

    /**
     * @orm\Id
     * @orm\Column(type="integer")
     * @orm\GeneratedValue
     */
    private $id;

    /**
     * @orm\ManyToOne(targetEntity="Hotels", inversedBy="photos")
     * @orm\JoinColumn(name="hotel_id", referencedColumnName="id")
     */
    private $hotel_id;

    /**
    * @return mixed
    */
    public function getHotelId()
    {
        return $this->hotel_id;
    }

    /**
    * @param mixed $hotel_id
    */
    public function setHotelId($hotel_id)
    {
        $this->hotel_id = $hotel_id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

}

在控制器中:

$hotel = $this->entityManager->getRepository('Hotels\Entity\Hotels')->find(1);

foreach ($hotel->getPhotos() as $photo) {
    echo $photo->getId() . "\n\n";
 }

我收到此错误:

PHP Fatal error:  Call to a member function setValue() on null in /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php

有人知道为什么会发生此错误以及如何解决该错误吗?我的映射有问题吗?

1 个答案:

答案 0 :(得分:0)

我认为在酒店类代码中有错误,您编写的是“酒店”而不是“ hotel_id”,我已更正了代码,如下所示:

/**
* One hotel has many features. This is the inverse side.
* @var ArrayCollection
* @orm\OneToMany(targetEntity="Photos", mappedBy="hotel_id")
* @orm\JoinColumn(name="id", referencedColumnName="hotel_id")
*/
private $photos;
相关问题