Question

Is there anyway to access the data of my entity without to use a specific accessor to my column value. Is there any generic accessor? See example:

$em = $this->getDoctrine()->getManager();
$data = $em->getRepository('EgBundle:Table')->findAll()

foreach($data as $row) {
  var_dump($row->get('col1')); // I would like to do this
  var_dump($row->getCol1()); // instead of this
  $col = 'getCol1'; var_dump($row->$col()); // this is my temporary solution
}
Was it helpful?

Solution

You might be able to make use of the symfony2 PropertyAccessor component.

Docs here: http://symfony.com/doc/current/components/property_access/introduction.html

Example:

$accessor = PropertyAccess::createPropertyAccessor();
$accessor->getValue($row, 'col1');

OTHER TIPS

You can get an array if you use DQL

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT table FROM EgBundle\Entity\Table table');
$data = $query->getQuery()->getArrayResult();

foreach ($data as $row) {
    var_dump($data['col1']);
}

No, not without adding support for that with userland code. You can add something like:

public function get($property) {
    return $this->$property;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top