Symfony和Doctrine:如何将具有多个子选择的查询转换为带有连接的dql

时间:2017-03-28 07:22:05

标签: sql symfony doctrine-orm

我需要一个远远超出我能管理的查询的帮助。该查询应该给我一个由货轮发送的包裹的价格。要获得价格,需要检查7个表格的地理区域,重量等级。以下是sql中的表:

用于获取所选货机价格的帐户表:

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
);

价格区域是为了使货轮能够为几个地理区域提供相同的价格

CREATE TABLE `price_zone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Geo区域是一个具有from和to区域的区域

CREATE TABLE `geo_zone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `from_zone_id` int(11) DEFAULT NULL,
  `to_zone_id` int(11) DEFAULT NULL,
  `price_zone_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_A4CCEF071972DC04` (`from_zone_id`),
  KEY `IDX_A4CCEF0711B4025E` (`to_zone_id`),
  KEY `IDX_A4CCEF07F40AF7F7` (`price_zone_id`),
  CONSTRAINT `FK_A4CCEF0711B4025E` FOREIGN KEY (`to_zone_id`) REFERENCES `zone` (`id`),
  CONSTRAINT `FK_A4CCEF071972DC04` FOREIGN KEY (`from_zone_id`) REFERENCES `zone` (`id`),
  CONSTRAINT `FK_A4CCEF07F40AF7F7` FOREIGN KEY (`price_zone_id`) REFERENCES `price_zone` (`id`)
);

区域是邮政编码的集合

CREATE TABLE `zone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
);

邮政编码已连接到货机帐户

CREATE TABLE `postal_code` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zone_id` int(11) DEFAULT NULL,
  `postal_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `postal_address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `account_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_EA98E3769F2C3FAB` (`zone_id`),
  KEY `IDX_EA98E3769B6B5FBA` (`account_id`),
  CONSTRAINT `FK_EA98E3769B6B5FBA` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`),
  CONSTRAINT `FK_EA98E3769F2C3FAB` FOREIGN KEY (`zone_id`) REFERENCES `zone` (`id`)
);

包装类型是托盘,盒子或类似物。它是系统范围的,而不是货机特定的

CREATE TABLE `package_type` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `default_length` int(11) DEFAULT NULL,
  `default_width` int(11) DEFAULT NULL,
  `default_height` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

重量等级是货轮的最小和最大重量的货机定义

CREATE TABLE `weight_class` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) DEFAULT NULL,
  `min_weight` int(11) DEFAULT NULL,
  `max_weight` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_D2EC4C2F9B6B5FBA` (`account_id`),
  CONSTRAINT `FK_D2EC4C2F9B6B5FBA` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`)
);

最后是将所有东西连接在一起的价格表

CREATE TABLE `price` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `package_type_id` int(11) DEFAULT NULL,
  `weight_class_id` int(11) DEFAULT NULL,
  `price_zone_id` int(11) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_CAC822D94A17E91F` (`package_type_id`),
  KEY `IDX_CAC822D9A0206A65` (`weight_class_id`),
  KEY `IDX_CAC822D9F40AF7F7` (`price_zone_id`),
  CONSTRAINT `FK_CAC822D94A17E91F` FOREIGN KEY (`package_type_id`) REFERENCES `package_type` (`id`),
  CONSTRAINT `FK_CAC822D9A0206A65` FOREIGN KEY (`weight_class_id`) REFERENCES `weight_class` (`id`),
  CONSTRAINT `FK_CAC822D9F40AF7F7` FOREIGN KEY (`price_zone_id`) REFERENCES `price_zone` (`id`)
);

A有一个有效的查询,但我觉得一个熟悉SQL的人正在努力让我头疼。

select p.price from price as p
where package_type_id = :packageType
and weight_class_id = (select id from weight_class where min_weight < :weight and max_weight > :weight)
and price_zone_id = (
    select price_zone_id from geo_zone
    where from_zone_id = (select zone_id from postal_code where account_id = :freighter and postal_code = :from)
    and to_zone_id = (select zone_id from postal_code where account_id = :freighter and postal_code = :to)

如何使用querybuilder在symfony和doctrine中完成这项工作?

感谢您阅读整个问题!

此致 托米

0 个答案:

没有答案