2013-02-03 43 views
0

我試着使用此查詢來獲取一個Symfony2的實體:從原材料MySQL查詢symfony的2

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; 
    } 
} 
+0

做你的實體的對應關係界定? –

+1

請問您是否可以發佈您的實體課程(禮物和禮物代碼) –

+0

我不是在處理這些實體的實體之間的關係。我只是想知道是否可以將原始查詢直接「翻譯」到symfony以獲取禮品實體。無論如何,給我30秒,並添加實體 – DomingoSL

回答

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沒有一個Gift擁有,你唯一需要做的:

$qtty= count($gift->getCodes()); 
+0

這個工程,但你如何獲取這個實體? – DomingoSL

+0

對不起,我的解決方案是錯誤的。現在它更好。 –