scootstah
scootstah

Reputation: 303

Doctrine2 using Native Query to join an unmapped Entity

I have two Entities: Workorder, and Photo. Photo is storing an image BLOB in each row. I need to be able to grab the latest photo in the Workorder, without manualling setting it for each Workorder result. I originally set up a OneToOne relationship in the Workorder to the Photo field, which I later with the QueryBuilder with a "WITH" clause to get the latest photo. This worked fine for that use case, however it seems that if I select a Photo and let the Workorder be joined (Photo has a ManyToOne with Workorder), then the Workorder went ahead and grabbed every Photo which was related. Considering the Photo stores a BLOB, that got expensive really fast.

I really only needed to join the latest Photo for one specific use case, but it needs to be part of the Workorder object. I thought that Native Queries would help me here, as I thought I would be able to map the joined object into a Workorder field, but I can't seem to make that work, or find any resources on this particular problem.

I have a field called $latest_photo in Workorder, but I have no mapping for it. I thought I would be able to map the joined Photo object into this field. Is this possible, or am I reading it wrong?

$rsm->addEntityResult('WorkOrder', 't0');
$rsm->addJoinedEntityResult('Photo', 't1', 't0', 'latest_photo');

This is what I've tried, but I just get a Hydrate error saying that "index latest_photo doesn't exist".

Upvotes: 2

Views: 592

Answers (1)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44851

Why don't you try Extra Lazy Associations before going with native queries?

Upvotes: 0

Related Questions