Doctrine 对null上的成员函数setValue()的ArrayCollection调用
我正在使用Zend Framework 3编写一个应用程序。为了管理数据库,我决定使用条令 我对教义有一个奇怪的问题。我建立了以下结构:Doctrine 对null上的成员函数setValue()的ArrayCollection调用,doctrine,zend-framework3,Doctrine,Zend Framework3,我正在使用Zend Framework 3编写一个应用程序。为了管理数据库,我决定使用条令 我对教义有一个奇怪的问题。我建立了以下结构: namespace Hotels\Entity; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as orm; /** * @orm\Entity * @orm\Tab
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;
}
}
以及:
在控制器中:
$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
有人知道为什么会发生这个错误吗?我如何修复它?我的映射有问题吗 我认为class Hotels代码有错误,您写的是“Hotels”而不是“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;
/**
* 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;