Symfony Teaching Undefined index in original request
I am trying to make a custom request on my FacebookEventResult object and create a connection to my FacebookEvent object.
Relevant mappings in FacebookEventResult:
/**
* @ORM\Column(type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="FacebookEvent", inversedBy="facebookEventResults")
* @ORM\JoinColumn(name="facebook_event_id", referencedColumnName="id", nullable=false)
**/
protected $event;
Relevant mappings in FacebookEvent:
/**
* @ORM\Column(type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="FacebookEventResult", mappedBy="event")
**/
protected $facebookEventResults;
My request code:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('AppBundle:FacebookEventResult', 'fer');
$rsm->addFieldResult('fer', 'id', 'id');
$rsm->addFieldResult('fer', 'event_date', 'eventDate');
// $rsm->addFieldResult('fer', 'facebook_event_id', 'event'); // this doesnt help
// some other mappings here as well, also tried without
$rsm->addJoinedEntityResult('AppBundle:FacebookEvent' , 'fe', 'fer', 'event');
$rsm->addFieldResult('fe', 'id', 'id');
// some other mappings here as well, also tried without
$sql = 'SELECT *
FROM facebook_event_result fer
INNER JOIN facebook_event fe ON fer.facebook_event_id = fe.id';
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();
On execution I get the following error:
Note: Undefined index: id
I tried following these instructions from Doctrine.
See: https://github.com/doctrine/doctrine2/issues/2482
I didn't see any permission for this on the github thread, but they suggested an alternative that worked for me: ResultSetMapping#addScalarResult($columnName, $alias)
So, something like this should work:
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('event_date', 'eventDate');
$sql = 'SELECT *
FROM facebook_event_result fer
INNER JOIN facebook_event fe ON fer.facebook_event_id = fe.id';
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();
NOTE: This became a temporary solution for me; it seems that their own examples don't work or require something that is missing and poorly documented.
If you forgot, add this to your script
use Doctrine\ORM\Query\ResultSetMapping;