我正在应用下面的查询使用querybuilder,但不知何故many2many关系不能按预期工作。多对多的关系querybuilder doctrine和symfony2扩展查询
$shopData = $sm->createQueryBuilder()
->select('v')
->from('AdminBundle:Voucher','v')
->innerJoin('v.shop', 's')
->leftJoin('AdminBundle:VoucherProgram', 'vp', \Doctrine\ORM\Query\Expr\Join::ON, 'vp.id = v.program_id')
->leftJoin('AdminBundle:shopHistory', 'sh', \Doctrine\ORM\Query\Expr\Join::ON, 'sh.shop = s.id')
->where('s.shopStatus = :shopStatus')
->setParameter('shopStatus', Shop::SHOP_ACTIVATED)
->andWhere('s.highlightedHome = :highlightedHome')
->setParameter('highlightedHome', Shop::SHOP_HIGHLIGHTED_HOME)
->andWhere('s.offers = \'voucher\'')
->setFirstResult(0)
->setMaxResults(6)
->addOrderBy('v.discount_amount', 'DESC')
->groupBy('sh.shop')
->getQuery()
->getSql();
生成的查询看起来象下面这样:
SELECT v FROM AdminBundle:Voucher v INNER JOIN v.shop s LEFT JOIN AdminBundle:VoucherPrograms vp ON vp.id = v.program_id LEFT JOIN AdminBundle:shopHistory sh ON sh.shop = s.id WHERE s.shopStatus = :shopStatus AND s.highlightedHome = :highlightedHome AND s.offers = 'voucher' GROUP BY sh.shop ORDER BY v.discount_amount DESC
在情况下,如果我删除一切,只保留内加入了Many2Many关系,它是按预期工作。
$sm->createQueryBuilder()
->select('v')
->from('AdminBundle:Voucher','v')
->innerJoin('v.shop', 's');
下面是生成的查询:
SELECT l0.* FROM voucher l0_ INNER JOIN shop_voucher l2_ ON l0_.id = l2_.voucher_id INNER JOIN shop l1_ ON l1_.id = l2_.shop_id;
所以我很奇怪,为什么系统不拿起正确的位置关系,当我添加更多的连接。
这里是具有Many2Many关系我的主要实体:
Shop.php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Shop.
*
* @ORM\Table(name="shop")
* @ORM\Entity(repositoryClass="AdminBundle\Entity\ShopRepository")
*/
class Shop
{
const SHOP_DEACTIVATED = 0;
const SHOP_ACTIVATED = 1;
const SHOP_HIGHLIGHTED_HOME = 1;
................................
/**
* @ORM\ManyToMany(targetEntity="Voucher", inversedBy="shop")
* @ORM\JoinTable(name="shop_voucher")
*/
private $voucher;
................................
Voucher.php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
#use Doctrine\Common\Collections\ArrayCollection;
/**
* Voucher.
*
* @ORM\Table(name="voucher")
* @ORM\Entity(repositoryClass="AdminBundle\Entity\VoucherRepository")
*/
class Voucher
{
................................
/**
* @ORM\ManyToMany(targetEntity="Shop", mappedBy="voucher", cascade={"persist"})
*/
private $shop;
................................
我已经检查堆栈有相同的问题,但我想知道我们如何扩展查询。对于我的问题,我有如下的解决方案,但没有完全确定上述情况中发生的情况。
$shopDataQuery = $connection->prepare('SELECT v.* FROM voucher AS v INNER JOIN shop_voucher AS sv ON sv.voucher_id = v.id INNER JOIN shop AS s ON s.id = sv.shop_id LEFT JOIN voucher_programs AS vp ON vp.id = v.program_id LEFT JOIN shop_history AS sh ON sh.shop = s.id WHERE s.shopStatus = :shopStatus AND s.highlightedHome = :highlightedHome AND s.offers = 'voucher' GROUP BY sh.shop ORDER BY v.discount_amount DESC LIMIT 6');
更新:
这里是shopHistory.php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* shopHistory.
*
* @ORM\Table(name="shop_history")
* @ORM\Entity(repositoryClass="AdminBundle\Entity\shopHistoryRepository")
*/
class shopHistory
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
* @ORM\ManyToOne(targetEntity="Shop", inversedBy="shopHistory")
* @ORM\JoinColumn(name="shop", referencedColumnName="id")
*/
private $shop;
这里是VoucherProgram.php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* VoucherProgram.
*
* @ORM\Table(name="voucher_program")
* @ORM\Entity(repositoryClass="AdminBundle\Entity\VoucherProgramRepository")
*/
class VoucherProgram
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Voucher", mappedBy="program")
*/
private $voucher;
/**
* @ORM\OneToMany(targetEntity="Shop", mappedBy="vprogram")
*/
private $shop;
检查' - > groupBy('sh.shop')'这个语句,这需要'select' clausule中的商店实体的列ID。 – yceruto
我已评论groupby,但结果相同或不完全符合您的意思。 – Kamal
你如何得到疑问? 第一个似乎是DQL查询(因此,如果您不强制连接列,则不指定连接列),第二个是SQL查询 – Seb33300