Hi,

On 30 July 2014 07:05, Diogo Domanski <[email protected]> wrote:

> I'm facing some troubles when filtering an entity association
> (ArrayCollection) by using Criteria.
>
> The scenario is the following: I have 3 entities:
>
> class Level
> {
>    ...
>    /**
>      * @ORM\ManyToMany(targetEntity="Agent")
>      * @ORM\JoinTable(name="level_agent",
>      *        joinColumns={@ORM\JoinColumn(name="level_id",
> referencedColumnName="id")},
>      *        inverseJoinColumns={@ORM\JoinColumn(name="agent_id",
> referencedColumnName="user_id")}
>      *    )
>      * @var \Doctrine\Common\Collections\ArrayCollection
>      */
>    private $agents;
>
>    public function __construct() {
>       $this->agents = new ArrayCollection();
>    }
>
>    public function getAgents() {
>       $criteria = Criteria::create()
>         ->where(Criteria::expr()->isNull("user.deleteDate"))
>         ->orderBy(array("user.name" => Criteria::ASC));
>
>         return $this->agents->matching($criteria);
>    }
>    ...
> }
>
> class Agent
> {
>    /**
>      *
>      * @ORM\OneToOne(targetEntity="User")
>      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
>      * @var User
>      */
>    protected $user;
>
>    /**
>      *
>      * @return User
>      */
>     public function getUser() {
>         return $this->user;
>     }
>    ...
> }
>
> class User
> {
>    /**
>      *
>      * @ORM\Id
>      * @ORM\Column(name="id", type="integer", nullable=false)
>      * @ORM\GeneratedValue(strategy="AUTO")
>      * @var integer
>      */
>     private $id;
>
>    /**
>      *
>      * @return int
>      */
>     public function getId() {
>         return $this->id;
>     }
>
>     ...
>
> }
>
>
> In DB, the "agent" table has a field called "user_id", that is a foreign
> key that references table "user". The relationship between "level" and
> "agent" occurs through "level_agent" - a n:m relationship.
>
> The problem is that whenever the getAgents() method (Level entity) is
> invoked, the list of associated agents is returned, however if I try to
> retrieve the respective user of any agent,
> it is NULL.
>
> For example:
>
> $agents = $levelEntity->getAgents();
>
> foreach($agents as $agent) {
>    $userId = $agent->getUser()->getId(); // This throws an error message
> saying that the result of getUser() is NULL
> }
>
>
> I've checked all related records at DB and everything is OK. So I decided
> to make a test and modify the getAgents() method by removing the filtering
> Criteria:
>
>     public function getAgents() {
>       return $this->agents;
>    }
>
>
> By simply doing that, the getUser() method started to return an entity of
> type User (as expected).
>
> Did anyone already face the same problem or know what is going wrong?
>

This looks like a bug to me (when applied to a
Doctrine\ORM\PersistentCollection), therefore I suggest opening a new issue
with a failing test demonstrating the problem.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to