在 Doctrine 1.2 oneToMany 关系中显示字符串值

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

我想显示字符串值而不是外来的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>

显示

 name   province
 Kansas 1(province_id)

我想要

 name    province
 KANSAS  SOUTH PROVINCE

在表类中

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

我的问题是我想回显 province_name 而不是 province_id

关于如何做到这一点有什么想法吗?

只需像这样修改您的 tableClass 查询

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);
}

现在您可以轻松显示字符串值而不是外国 ID

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