xavi
xavi

Reputation: 147

Doctrine Entity with two fields with fetch Eager

I have been struggling with this problem for a couple of days and I can't find any reason for it. I have a User Entity

 /**
 * @var string
 *
 * @ORM\Column(name="uuid", type="string", length=255, nullable=false)
 */
private $uuid;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255, nullable=false)
 */
private $name; 

/**
 * One User has Many Groups.
 *
 * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="user", cascade={"persist"}, fetch="EAGER")
 */
protected $groups;

/**
 * One User has Many Comments.
 *
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="user_id", fetch="EAGER")
 */
protected $comments;

User has two referenced entities: Groups (through a Pivot Entity UserGroup) and Comments

The problem is that when I perform the query, only one of the referenced entities content is obtained. In the above class it will only get "groups". If I remove Fetch Eager from $groups, it will gather "comments". If I put $comments property above "groups" it will recover only "comments" for this user, not groups.

If I try with Fetch Lazy and then I initialize() the property ("subentity" group or comment) and I do getSnapshot() happens the same.

Any idea why this can happen?

Many thanks!

Upvotes: 1

Views: 328

Answers (1)

xavi
xavi

Reputation: 147

So the problem was that the ID field of the entity is a stream (binary). When doctrine reads the stream once, it "empties" the stream, so on next usage, ID has no content. I solved it by re-setting the ID value to the entity from a backup variable

Upvotes: 1

Related Questions