设置产品时,我可以选择将其可用性限制为客户所在的国家,地区和城市。基于此,我只会向系统中符合此条件的相关客户展示产品。
在某些情况下,我只想将产品设置为一个国家/地区而忽略城市,我想知道如何最好地编写此查询。
我的看法有两种选择。
- 如果某个产品与所有城市和地区都相关,则只需将它们全部包含在内(大量数据)。
- 如果不需要城市/地区,只需忽略限制城市/城市的声明。
产品:
+---------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| uuid | binary(16) | NO | UNI | NULL | |
+---------------------+------------------+------+-----+---------+----------------+
很多到很多位置
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| product_id | int(10) unsigned | NO | PRI | NULL | |
| meta_location_id | int(11) | NO | PRI | NULL | |
+------------------+------------------+------+-----+---------+-------+
位置
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| iso | varchar(50) | YES | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
查询建筑商代码
$qb = $this->repository->createQueryBuilder('product');
$qb->orderBy('product.id','ASC');
//If we are limiting by country
if (isset($options['country']) and null !== $options['country']) {
$qb->andWhere('product.country = :country')
->setParameter('country',$options['country']);
}
地区/城市:
region选项是客户的当前区域,无论是否发送,我都无法控制。由于我在产品表中进行搜索,因此需要检查产品区域是否存在。如果确实如此,那么我必须将其限制在现有区域内,否则将其忽略。
当前查询: 对于此查询,如果区域不匹配,则不会列出产品。我想将其更改为仅适用于产品表中存在数据的情况。
if (isset($options['region']) and null !== $options['region']) {
$qb->leftJoin('product.regions','region');
->andWhere(($qb->expr()->in('region',[$options['region']])));
}
我猜我需要一些东西
$qb->add('select','CASE WHEN region.id IS NOT NULL then do some query...
任何想法将不胜感激!