Neo4j-PHP-OGM icon indicating copy to clipboard operation
Neo4j-PHP-OGM copied to clipboard

weird array collection behaviour

Open bazo opened this issue 12 years ago • 5 comments

i'm checking if a collection of liked venues contains a particular venue. at the time of the check the collection appears empty. i have to call the getLikedVenues() method, after that the collection is initialized and the check returns true. the venue i check for definitely belongs to the collection.

/** @OGM\ManyToMany(relation="like") */ 
$likedVenues

public function __construct()
{
    $this->likedVenues = new Doctrine\Common\Collections\ArrayCollection;
}

public function likesVenue(Venue $venue)
{
    $this->getLikedVenues(); //until i call this, the dump returns empty collection
    dump($this->likedVenues);
    return $this->likedVenues->contains($venue); //returns false if getLikedVenues() is not called.
}

public function getLikedVenues()
{
    return $this->likedVenues;
}

bazo avatar Jul 22 '12 20:07 bazo

this is also true for other collections, i thought this had been already resolved.

bazo avatar Jul 22 '12 20:07 bazo

yep, the collections are not initialized. and don't get initialized when accessing them. which is weird because i think it had worked before. maybe it has to do something with the new doctrine common version.

bazo avatar Jul 22 '12 20:07 bazo

This is based on how the proxy is implemented at this time. The loading occurs when a public call is made with a method that matches the property name.

I can think of a way around it for array collections, but it would make the behavior inconsistent with the single object. Calling the get method does not seem like a big deal.

lphuberdeau avatar Jul 22 '12 21:07 lphuberdeau

maybe. i'm sure this works in other doctrine libraries without calling the getter.

bazo avatar Jul 23 '12 07:07 bazo

It depends on the fetchMode as far as I can remember. I had run into issues as well with Doctrine when accessing properties directly. I verified the way doctrine proxies are generated and they are very similar. The only difference is that they seem to load everything at once while I load it per-property.

lphuberdeau avatar Jul 23 '12 11:07 lphuberdeau