简体   繁体   中英

Doctrine query in Symfony 1.4, not sure what I am doing wrong here

I have a query that reads like this:

$fields = Doctrine_Core::getTable('model_1')->findByColumnId($id); 

took a var_dump on fields, and it retrieves data as it should;

next up, I do this

foreach($fields as $field)
{
 $newerFields[$field->getColumnId()] =
 Doctrine_Core::getTable('model2')->findByColumn2Id($field->getColumnId());
}

I take a var_dump on each $newerFields[$field] and it does yield the result as it should. Next up I try and do this:

foreach($newerFields as $newerField)
{
 echo  $newerField->getColumn3();
}

But this spits out: Call to undefined method Doctrine_Collection::getColumn3() in actions.class.php on line 69

I am not able to wrap my head around this and am flustered thinking on how to get it done. Can anyone spot the trouble for me?

Your findByColumn2Id returns a Doctrine_Collection because findBy always returns a collection (if you don't define it in the table file) even if there is only one result. If you want to retrieve one result, use findOneBy instead.

Otherwise, you will have to loop on newerFields childs to retrieve data:

// array of Doctrine_Collection
foreach($newerFields as $newerFieldCollection)
{
  foreach($newerFieldCollection as $newerField)
  {
    echo $newerField->getColumn3();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM