symfony-1.4doctrine-1.2

Displaying string value in Doctrine 1.2 oneToMany relationship


city
  columns
  id
  name
  province_id
Relation
  Province
  one to many
Province
  columns 
  id
  province_name

I want to display the string value instead of foreign id.So far

<?php foreach($results as $r): ?>
    <tr>
        <td><?php echo $r['name'] ?></td>
        <td><?php echo $r['province_id']?></td>
    </tr>
<?php endforeach ?>
</tbody>

displays

 name   province
 Kansas 1(province_id)

I want instead

 name    province
 KANSAS  SOUTH PROVINCE

in tableClass

public function SearchCity($name)
{
    return $this->createQuery('c')
    ->andWhere('c.name like ?', '%'.$name.'%')
    ->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
}

my problem is I want to echo province_name instead of province_id

Any Idea on how to do this?


Solution

  • Just modify your tableClass query like this

    public function SearchCity($name)
    {
       return $this->createQuery('c')
        ->select('c.*')
        ->innerJoin('c.Province p')
        ->addSelect('p.name as provincename')
        ->andWhere('c.name like ?', '%'.$name.'%')
        ->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
    }
    

    And now you can easily display string values instead of foreign ids

    <td><?php echo $r['name'] ?></td>
    <td><?php echo $r['provincename'] ?></td>