0

我正在尝试使用此查询获取 symfony2 实体:

SELECT g . *, 
       (SELECT Count(gc.`code`) AS `codecount` 
        FROM   giftcode gc 
        WHERE  gc.`gift_id` = g.`id` 
               AND gc.`provided` IS NULL 
        GROUP  BY gc.`gift_id`) AS gcodecount 
FROM   gift g 
WHERE  Now() >= g.validbegin 
       AND Now() <= g.validend 

到目前为止,我有这个:

$query = $giftRepository->createQueryBuilder('p')
    ->where(':now >= p.validbegin AND :now <= p.validend')
    ->setParameter('now', new \DateTime())
    ->orderBy('p.points', $hts)
    ->getQuery();
    $gifts = $query->getResult();

它工作但不完整,我不知道如何在查询中调用第二个表(giftcode),就像我在原始查询中所做的那样。

编辑

namespace Done\PunctisBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Gift
 *
 * @ORM\Table(name="gift")
 * @ORM\Entity
 */
class Gift
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="redemption", type="string", nullable=true)
     */
    private $redemption;


    /**
     * @var \Date $validbegin
     *
     * @ORM\Column(name="validbegin", type="date", nullable=true)
     */
    private $validbegin;

    /**
     * @var \Date $validend
     *
     * @ORM\Column(name="validend", type="date", nullable=true)
     */
    private $validend;

    /**
     * @var integer
     *
     * @ORM\Column(name="amount", type="integer", nullable=true)
     */
    private $amount;

    /**
     * @var integer
     *
     * @ORM\Column(name="points", type="integer")
     */
    private $points;

    /**
     * @var boolean
     *
     * @ORM\Column(name="verified", type="boolean")
     */
    private $verified;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255)
     */
    private $image;

    /**
     * @var string
     *
     * @ORM\Column(name="price", type="integer")
     */
    private $price;

    /**
     * @var string
     *
     * @ORM\Column(name="discount", type="integer")
     */
    private $discount;

    /**
     * @var \Done\PunctisBundle\Entity\Brand
     *
     * @ORM\ManyToOne(targetEntity="Done\PunctisBundle\Entity\Brand", inversedBy="gifts")
     */
    protected $brand;

    /**
     * @var \Done\PunctisBundle\Entity\GiftCode
     *
     * @ORM\OneToMany(targetEntity="Done\PunctisBundle\Entity\GiftCode", mappedBy="gift", cascade={"persist"})
     */
    protected $codes;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->codes    = new \Doctrine\Common\Collections\ArrayCollection();
        $this->verified = false;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Gift
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Gift
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }


    /**
     * Set redemption
     *
     * @param string $redemption
     * @return Gift
     */
    public function setRedemption($redemption)
    {
        $this->redemption = $redemption;

        return $this;
    }

    /**
     * Get redemption
     *
     * @return string 
     */
    public function getRedemption()
    {
        return $this->redemption;
    }


    /**
     * Set points
     *
     * @param integer $points
     * @return Gift
     */
    public function setPoints($points)
    {
        $this->points = $points;

        return $this;
    }

    /**
     * Get points
     *
     * @return integer 
     */
    public function getPoints()
    {
        return $this->points;
    }



    /**
     * Set amount
     *
     * @param integer $amount
     * @return Gift
     */
    public function setAmount($amount)
    {
        $this->amount = $amount;

        return $this;
    }

    /**
     * Get amount
     *
     * @return integer 
     */
    public function getAmount()
    {
        return $this->amount;
    }





    /**
     * Set verified
     *
     * @param boolean $verified
     * @return Gift
     */
    public function setVerified($verified)
    {
        $this->verified = $verified;

        return $this;
    }

    /**
     * Get verified
     *
     * @return boolean 
     */
    public function getVerified()
    {
        return $this->verified;
    }

    /**
     * Set image
     *
     * @param string $image
     * @return Gift
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string 
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set price
     *
     * @param integer $price
     * @return Gift
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return integer 
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set discount
     *
     * @param integer $discount
     * @return Gift
     */
    public function setDiscount($discount)
    {
        $this->discount = $discount;

        return $this;
    }

    /**
     * Get discount
     *
     * @return integer 
     */
    public function getDiscount()
    {
        return $this->discount;
    }

    /**
     * Set brand
     *
     * @param \Done\PunctisBundle\Entity\Brand $brand
     * @return Gift
     */
    public function setBrand(\Done\PunctisBundle\Entity\Brand $brand = null)
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * Get brand
     *
     * @return \Done\PunctisBundle\Entity\Brand 
     */
    public function getBrand()
    {
        return $this->brand;
    }

    /**
     * Add codes
     *
     * @param \Done\PunctisBundle\Entity\GiftCode $codes
     * @return Gift
     */
    public function addCode(\Done\PunctisBundle\Entity\GiftCode $codes)
    {
        $this->codes[] = $codes;

        return $this;
    }


    /**
     * Set validbegin
     *
     * @param \Date $validbegin
     * @return UserInfo
     */
    public function setValidbegin($validbegin)
    {
        $this->validbegin = $validbegin;

        return $this;
    }

    /**
     * Get validbegin
     *
     * @return \Date 
     */
    public function getValidbegin()
    {
        return $this->validbegin;
    }


    /**
     * Set validend
     *
     * @param \Date $validend
     * @return UserInfo
     */
    public function setValidend($validend)
    {
        $this->validend = $validend;

        return $this;
    }

    /**
     * Get validend
     *
     * @return \Date 
     */
    public function getValidend()
    {
        return $this->validend;
    }




    /**
     * Remove codes
     *
     * @param \Done\PunctisBundle\Entity\GiftCode $codes
     */
    public function removeCode(\Done\PunctisBundle\Entity\GiftCode $codes)
    {
        $this->codes->removeElement($codes);
    }

    /**
     * Get codes
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCodes()
    {
        return $this->codes;
    }
}

<?php

namespace Done\PunctisBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * GiftCode
 *
 * @ORM\Table(name="giftcode")
 * @ORM\Entity
 */
class GiftCode
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=255)
     */
    private $code;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="provided", type="datetime", nullable=true)
     */
    private $provided;

    /**
     * @var \Done\PunctisBundle\Entity\Gift
     *
     * @ORM\ManyToOne(targetEntity="Done\PunctisBundle\Entity\Gift", inversedBy="codes")
     */
    protected $gift;


    public function __construct( )
    {
        $this->provided = null;
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set code
     *
     * @param string $code
     * @return GiftCode
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return string 
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * Set provided
     *
     * @param \DateTime $provided
     * @return GiftCode
     */
    public function setProvided($provided)
    {
        $this->provided = $provided;

        return $this;
    }

    /**
     * Get provided
     *
     * @return \DateTime 
     */
    public function getProvided()
    {
        return $this->provided;
    }

    /**
     * Set user
     *
     * @param string $user
     * @return GiftCode
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return string 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set gift
     *
     * @param \Done\PunctisBundle\Entity\Gift $gift
     * @return GiftCode
     */
    public function setGift(\Done\PunctisBundle\Entity\Gift $gift = null)
    {
        $this->gift = $gift;

        return $this;
    }

    /**
     * Get gift
     *
     * @return \Done\PunctisBundle\Entity\Gift 
     */
    public function getGift()
    {
        return $this->gift;
    }
}
4

1 回答 1

0

您可以尝试以下方法:

$query=$entityManager->createQuery("SELECT g
                                      FROM gift g  
                                     WHERE  Now() >= g.validbegin 
                                        AND Now() <= g.validend 
                                        AND gc.provided IS NULL");
 $list=$query->getResults();

现在你可以遍历$list并且每个元素都是一个Gift.

Giftcodes要知道a有多少Gift,您只需要执行以下操作:

 $qtty= count( $gift->getCodes());
于 2013-02-03T19:59:57.237 回答