【发布时间】:2015-11-21 03:22:11
【问题描述】:
我正在为我的公司实体创建一个搜索功能,并使用 Symfony 2 和 Doctrine ORM 将我的逻辑放入自定义实体存储库中。
公司实体有一个子集合 Locations,因为公司可以有多个位置,以及一个公司名称字段。
我想知道是否可以使用查询生成器按公司名称和 Location[1].address、Location[2].address 等进行搜索。
这是我到目前为止所拥有的,找不到学习这个的好资源。
*/ AppBundle/Repository/CompanyRepository.php */
class CompanyRepository extends EntityRepository
{
public function findBySearch($options)
{
$query = $this->createQueryBuilder('c');
foreach ($options as $key => $value) {
$query->where('c.' . $key . ' LIKE :' . $key);
$query->setParameter($key, '%' . $value . '%');
}
return $query->getQuery()->getResult();
}
}
看起来这不起作用:
$query->where('c.locations.city LIKE $city);
findBySearch() 中的 $options 参数示例如下:
[
'companyName' => 'Mikes Company',
'locations.city' => 'New York',
'locations.state' => 'New York'
]
非常感谢任何建议或资源链接。
【问题讨论】:
标签: symfony search doctrine-orm