views:

246

answers:

2

Hi,

I'm trying to use an SQL query to get data from my database into the template of a symfony project.

my query:

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 acces this data in the template by either doing:

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

or

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

i get the 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 dont know how to call the value from the array if the names in both query's dont work.

+1  A: 

Doctrine will have hydrated your results into objects corresponding to the models in your query. In your case these will be Locatie and Organisatie. You should therefore be able to access the data as follows:

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

If you have the above method in eg the Locatie table class and use self::create("l") to create your method, the object you use in the view won't need the ->Locatie part.

Edit: table method example:

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 should be able to find this class (probably empty) already auto-generated in lib/model/doctrine/LocatieTable.class.php. Now call it with:

$this->q = Doctrine::getTable("Locatie")->getLocaties();
richsage
Hi, thank you for your help, i cant get it to work though, if i try your foreach symfony still trows an error: Unknown record property / related component "Locatie" on "Locatie"i currently have the query in my action.
iggnition
if i remove the first line and just try the second (organisatie) i indeed does correctly show the right name, but i do get a notice/warning: Notice: Trying to get property of non-object in //file location//
iggnition
I'd advise moving the query to your Locatie model and trying that way - using the 2nd part of my answer. Let me know if you'd like an example :-) Alternatively, try var_dump()'ing out the objects ($obj) and you'll see how it's structured.
richsage
Thank you, Ill try that :), also i found some doctrine documentation on hydration so that will help too. Ill check back after i try your changes :)
iggnition
Just wondering, how to use the self::create("l"), culd you give me an example?this is what i have:http://pastebin.com/cQzxpEP8
iggnition
Updated to include an example :-)
richsage
I dont see it :PI did get it to work btw, the query is still in the action, but it works. The error i posted about is because the organisation can be NULL, i added a check to see if its set before php echoes it and now it works. I would still like to see how i can put the query in my model though ;)
iggnition
Thanks for accepting :-) you can't see the edit I made to my answer? There's an example about using the query inside the Doctrine table class for your model.
richsage
Ah, I see it now, stupid me. Thank you very much for your help! It works :D
iggnition
A: 

If you want to know how to get some value from the result DoctrineRecord object I advise to use var_dump($obj->toArray()) method to get clear view of the object structure. After that you can use several types of getters to retrive what you want (e.g. $obj->A->b, $obj->getA()->getB() etc..)

AlekSander
Thank you for you comment, this actually helped me an a completely different problem :)
iggnition