Marquis Taylor
Marquis Taylor

Reputation: 103

How do I hydrate and Entity with extra data

So I have a query that gets distance to a site. The query returns the entities sites correctly, and it calculates the distance. This is my query.

$query = $this->getEntityManager()->createQuery('SELECT s, ROUND( ( :milesOrKm * acos( cos( radians(:myLat) ) * cos( radians( s.latitude ) ) * cos( radians( s.longitude ) - radians(:myLong) ) + sin( radians(:myLat) ) * sin( radians( s.latitude ) ) ) )) AS distance FROM AppBundle:Site s ORDER By distance ASC')->setParameter('milesOrKm', $units)

When the results come in, they are an array with my site, but also a distance. This is close to what I need, but I want the distance field added to my entity even though it's not a stored doctrine field.

array:2 [▼
  0 => array:2 [▼
    0 => Site {#512 ▶}
    "distance" => "1"
  ]
  1 => array:2 [▼
    0 => Site {#521 ▶}
    "distance" => "2649"
  ]
]

Basically I want

array:2 [▼
    0 => Site {#512 ▶}
    1 => Site {#521 ▶}
]

How do I add the distance to my entity? I have a distance protected property (with no annotation so it's not loaded and saved).

I suspect I need a custom hydration, but I'm having trouble figuring that out. Any suggestions?

Upvotes: 0

Views: 612

Answers (1)

kamwoz
kamwoz

Reputation: 226

What you need is your own custom hydrator. Here is good example of how to do it

Upvotes: 0

Related Questions