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

时间:2015-07-19 17:07:06

标签: symfony-1.4 doctrine-1.2

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
tableClass中的

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

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

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:2)

只需像这样修改你的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>