I have 2 entities : User, Article and a many-to-many relationship with attribute to describe the User interactions with the Articles . The attribute describe the kind of interaction (like, bookmark...).
In this case, i need an intermediate entity (UserArticle) with a One-to-many relationship from Article towards UserArticle : the property is called userInteractions in the Article entity.
It's an API and when I create the Query Builder, I get this kind of result for an Article that has been liked and bookmarked by the current logged-in user:
//Article Json Object
{
...
userInteractions : [
{
"type": "like"
},
{
"type": "bookmark"
}
]
}
That's OK but not convenient for the javascript frontend : I would prefer to expose boolean fields : isLiked : true, bookmarked: true, otheraction: false ... , because it is easier to parse.
I thought I had to write a custom hydrator. I did it for another relationship (many-to-many without attributes) and it works like a charm but in this case it works only when there is 0 or one interaction. If there are 2 or more actions, I get a 500 error and a notice when I call the parent Doctrine Object hydrator. Here's my hydrator :
class ArticleHydrator extends ObjectHydrator {
protected function hydrateRowData(array $data, array &$result)
{
$hydratedResult = [];
parent::hydrateRowData($data, $hydratedResult);
//this call generates a notice Undefined offset: 0 at line 525 of doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
$article->setUserInteractionsFlags();
// works with less than 2 interactions in the left join and correctly hydrates the not-persisted booleans added to Article entity
}
}
I tried to look in the Object hydrator but it's a real mess and not easily understandable and I thought that maybe, in this case using a custom hydrator is not appropriate. Any advise ? Thanks