问题描述
嘿,使用带有属性 GeoData 的 Doctrine ORM
/**
* @var GeoData[]|Collection
*
* @ORM\ManyToMany(targetEntity="Acme\Bundle\CoreBundle\Entity\GeoData",inversedBy="addresses",cascade="persist",fetch="LAZY")
* @ORM\JoinTable(name="users__addresses_geodata")
*/
protected $geoData;
并使用特殊的 getter 来获取 GeoData 的第一个元素
public function getMostLocalGeoDatum(): ?GeoData
{
if (null == $this->geoData && $this->geoData->isEmpty()){
return null;
}
/** @var GeoData $localeGeoDatum */
$localeGeoDatum = $this->geoData->first();
return $localeGeoDatum;
}
TypeError: Return value of Acme\Bundle\CoreBundle\Entity\Address::getMostLocalGeoDatum() must be an instance of Acme\Bundle\CoreBundle\Entity\GeoData or null,boolean returned
任何线索可能是错误的?根据 Doctrine ArrayCollection 文档,first() 应该返回 ArrayCollection 的第一个元素。
解决方法
第一个 if
不应为 and
,而应为 or
条件。