Found it in the doctrine docs here: http://docs.doctrine-project.org/en/latest/reference/working-with-objects.html
I need simply invoke $em->clear();
to reset the mapping and the full entity is hydrated in the subsequent DQL. Yay!
Pregunta
I'm using Doctrine2 in Zend Framework 2. In a very unique case, I need to create a native SQL query which only selects a table's id and does some geographic calculations. I followed the documentation and created a ResultSetMapping definition for this query:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Location', 'l');
$rsm->addFieldResult('l', 'id', 'id');
$rsm->addScalarResult('distance', 'distance');
$query = $em->createNativeQuery('SELECT l.id, (3959 * acos(cos(radians(:lat))
* cos( radians( l.latitude ) )
* cos( radians( l.longitude ) - radians(:lng) )
+ sin( radians(:lat) )
* sin( radians( l.latitude ) ) ) ) AS distance
FROM location l
WHERE
l.latitude BETWEEN (:lat - .2) AND (:lat + .2)
AND l.longitude BETWEEN (:lng -.2) AND (:lng + .2)
HAVING distance < 10 ORDER BY distance'), $rsm);
$query->setParameter('lng', $lng)
->setParameter('lat', $lat)
This returns a result set correctly and I process it.
However, I want to return a fully hydrated collection of those entities so I do a simple DQL query
$em->createQuery('SELECT l FROM Location l WHERE l.id IN (:locations)')
->setParameter('locations', $locationIds)
->getResult();
The resulting collection of entities only has the 'id' field hydrated. All other values are null.
I suspect this is because I set the ResultSetMapping for the previous query and it's being applied in the DQL afterwards as well. How do I go about resetting Doctrine's mapping behavior back to default?
Edit
Here is a truncated version of the Location entity. It hydrates just fine in all other areas of the site.
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="location")
*/
class Location extends RestrictedSite
{
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Annotation\Exclude()
*
* @var int
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, name="name")
*
* @var string
*/
protected $name;
/**
*
* @ORM\Column(type="float")
*
* @var float
*/
protected $latitude;
/**
*
* @ORM\Column(type="float")
*
* @var float
*/
protected $longitude;
... more columns & methods ...
}
The schema is something like:
CREATE TABLE `location` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`latitude` float DEFAULT NULL,
`longitude` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
Solución
Found it in the doctrine docs here: http://docs.doctrine-project.org/en/latest/reference/working-with-objects.html
I need simply invoke $em->clear();
to reset the mapping and the full entity is hydrated in the subsequent DQL. Yay!