您好,我正在尝试将ZF查询生成器重写为准则ORM。>>
这里是我的旧Zend框架查询。
$dependent = new Zend_Db_Expr('if(br.billing_rate = \'dependent\',(SELECT COUNT(*) FROM childcare_billing_rule_price AS p WHERE p.id < brp.id AND p.childcare_billing_rule_id = brp.childcare_billing_rule_id),0)'); $select = $this ->select()->setIntegrityCheck(false) ->from(array('brg' => 'childcare_billing_rule_group'), array()) ->joinInner(array('br' => 'childcare_billing_rule'), 'br.id = brg.billing_rule_id', array('*', 'dependent' => $dependent)) ->join(array('brp' => 'childcare_billing_rule_price'), 'br.id = brp.childcare_billing_rule_id', array('age_from', 'age_to', 'default_rate' => 'rate')) ->where('is_default != 1') ->where('br.billing_type in (\'' . implode('\',\'', array(self::BILLING_TYPE_PER_DAY, self::BILLING_TYPE_PER_HOUR, self::BILLING_TYPE_PER_UNLIMITED, self::BILLING_TYPE_PER_RESERVATION)) . '\')') ->where('brg.group_id IN (?)', $groupId) ->group('brp.id') ->having('dependent <= ?', $dependentLevel) ->order('dependent desc') ->order('brp.rate');
这里是SQL行值
SELECT `br`.*, if (br.billing_rate = 'dependent', (SELECT COUNT(*) FROM childcare_billing_rule_price AS p WHERE p.id < brp.id AND p.childcare_billing_rule_id = brp.childcare_billing_rule_id), 0) AS `dependent`, `brp`.`age_from`, `brp`.`age_to`, `brp`.`rate` AS `default_rate` FROM `childcare_billing_rule_group` AS `brg` INNER JOIN `childcare_billing_rule` AS `br` ON br.id = brg.billing_rule_id INNER JOIN `childcare_billing_rule_price` AS `brp` ON br.id = brp.childcare_billing_rule_id WHERE (is_default != 1) AND (br.billing_type in ('Per Day','Per Hour','Unlimited','Per Reservation')) AND (brg.group_id IN (103)) AND ((brp.age_from = 0 AND brp.age_to = 0) || 3.1666666666667 BETWEEN brp.age_from AND brp.age_to) GROUP BY `brp`.`id` HAVING (dependent <= '0') ORDER BY `dependentw` desc, `brp`.`rate` ASC
我不确定如何实现此子查询
new Zend_Db_Expr('if(br.billing_rate = \'dependent\',(SELECT COUNT(*) FROM childcare_billing_rule_price AS p WHERE p.id < brp.id AND p.childcare_billing_rule_id = brp.childcare_billing_rule_id),0)');
到目前为止,我的代码
$subQuery = $subQueryBuilder ->select('if (br.billing_rate = 'dependent', (SELECT COUNT(*) FROM childcare_billing_rule_price AS p WHERE p.id < brp.id AND p.childcare_billing_rule_id = brp.childcare_billing_rule_id)') ->getSQL(); $queryBuilder->select($alias) ->addSelect($subQuery) ->innerJoin(ChildcareBillingRule::class, 'br', Join::WITH, 'br.id = childcare_billing_rule_group.childcareBillingRule') ->innerJoin(ChildcareBillingRulePrice::class, 'brp', Join::WITH, 'br.id = brp.childcareBillingRule') ->where("br.billingType in (:billingTypes)") ->andWhere( 'br.isDefault != :isDefault') ->andWhere( 'childcare_billing_rule_group.groupId in (:groupId)') ->setParameter('groupId', [103]) ->setParameter('isDefault', 1) ->setParameter('billingTypes', ['Per Hour', 'Per Reservation', 'Per Day', 'Unlimited']) ->having('dependent <= ?', $dependentLevel') ->groupBy('brp.id') ->orderBy('brp.rate') ; ->getQuery()->getSingleResult();
所以我试图用addSelect添加子查询,但不确定这是正确的方法吗?任何帮助或想法表示赞赏吗? Tnx
您好,我正在尝试将ZF查询生成器重写为准则ORM。这是我的旧Zend框架查询。 $ dependent = new Zend_Db_Expr('if(br.billing_rate = \'dependent \',(SELECT COUNT(*)FROM ...
简而言之,而不是-> getSQL(),您应该使用-> getDQL()。