Doctrine ArrayCollection->first() returns boolean

2.8k Views Asked by At

Hey using Doctrine ORM with attribute GeoData

    /**
     * @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;

and with a special getter to acquire first element of GeoData

    public function getMostLocalGeoDatum(): ?GeoData
    {
        if (null == $this->geoData && $this->geoData->isEmpty()){
            return null;
        }
        /** @var GeoData $localeGeoDatum */
        $localeGeoDatum = $this->geoData->first();

        return $localeGeoDatum;
    }

but everytime I use this getter, I get a error:

TypeError: Return value of ACME\Bundle\CoreBundle\Entity\Address::getMostLocalGeoDatum() must be an instance of ACME\Bundle\CoreBundle\Entity\GeoData or null, boolean returned

Any clue what might be wrong? According to Doctrine ArrayCollection documentation, first() should return first element of ArrayCollection.

1

There are 1 best solutions below

0
Tarik Weiss On

The first if should not be and, instead it should be an or condition.