2

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.

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

9 Comments

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.
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//
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.
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 :)
Just wondering, how to use the self::create("l"), culd you give me an example? this is what i have:pastebin.com/cQzxpEP8
|
0

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..)

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.