ArrayCollection 在 null 上调用成员函数 setValue()
ArrayCollection Call to a member function setValue() on null
我正在使用 Zend Framework 3 编写应用程序。为了管理数据库,我决定使用 Doctrine。
我对 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
有人知道为什么会出现这个错误吗?我该如何解决?我的映射有问题吗?
我认为 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;
我正在使用 Zend Framework 3 编写应用程序。为了管理数据库,我决定使用 Doctrine。
我对 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
有人知道为什么会出现这个错误吗?我该如何解决?我的映射有问题吗?
我认为 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;