Symfony get data from array

I am trying to use an SQL query to get data from my database into a symfony project template.

my request:

SQL:
SELECT l.loc_id AS l__loc_id, l.naam AS l__naam, l.straat AS l__straat, 
l.huisnummer AS l__huisnummer, l.plaats AS l__plaats, l.postcode AS l__postcode,
l.telefoon AS l__telefoon, l.opmerking AS l__opmerking, o.org_id AS o__org_id, o.naam AS o__naam 
FROM locatie l 
LEFT JOIN organisatie o 
ON l.org_id = o.org_id

      

This is generated by this DQL:

DQL:
 $this->q = Doctrine_Query::create()
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->from('Locatie l')
->leftJoin('l.Organisatie o')
->execute();

      

But now when I try to get this data in the template by doing:

<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o.naam'] ?>

      

or

<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o__naam'] ?>

      

i am getting error from symfony:

500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "o__naam" on "Locatie"

      

Does anyone know what is going wrong here? I don't know how to call the value from the array if the names in both queries don't work.

+2


a source to share


2 answers


Doctrine will moisten your results in objects that match your query models. In your case, it will be Locatie

and Organisatie

. Therefore, you should be able to access the data like this:

<?php foreach ($q as $obj): ?>
  <?php echo $obj->Locatie->naam; ?>
  <?php echo $obj->Organisatie->naam; ?>
<?php endforeach; ?>

      

If you have the above method in a table class for example Locatie

and use self::create("l")

to create your method, the object you are using in the view will not need the part ->Locatie

.

Edit : Example of a table method:



class LocatieTable extends Doctrine_Table
{
  public function getLocaties()
  {
      $q = self::createQuery("l")
      ->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
      ->leftJoin('l.Organisatie o')
      ->execute();

      return $q;
  }      
}

      

You can find this class (possibly empty) already autogenerated in lib/model/doctrine/LocatieTable.class.php

. Now call it with:

$this->q = Doctrine::getTable("Locatie")->getLocaties();

      

+1


a source


If you want to know how to get any value from the DoctrineRecord result object, I recommend using the var_dump ($ obj-> toArray ()) method to get a clear idea of ​​the structure of the object. After that, you can use multiple types of getters to fetch what you want (e.g. $ obj-> A-> b, $ obj-> getA () -> getB (), etc.)



0


a source







All Articles