2015-01-16 171 views
0

我的Symfony2項目中的查詢有一個奇怪的問題。學說只從數據庫返回單個結果,即使它應該返回多個記錄

我有以下查詢:

 $enviroFigures = $dm->createQuery(' 
      SELECT efu.wasteType, efu.ewcCode, SUM(efu.totalUom) AS totalTonne, SUM(efu.co2Saving) AS totalCO2 
      FROM CoreBundle:EnviroFiguresUpload efu 
      WHERE efu.customerSite = :site 
      AND efu.completionDate BETWEEN :start AND :end 
      AND efu.completionDate != :blank 
      AND efu.totalUom != :zero 
      AND efu.wasteType != :blank 
      ORDER BY efu.completionDate DESC' 
     )->setParameters(array(
      'site' => $cs, 
      'start' => $ds, 
      'end' => $de, 
      'blank' => '', 
      'zero' => '0' 
     )); 

     $enviFig = $enviroFigures->getResult(); 

,用測試數據我有,人有兩個記錄該查詢將返回數據庫。我甚至使用這個SQL查詢來測試一般查詢,並且它已經按預期返回了兩行數據。

SELECT waste_type AS wasteType, ewc_code AS ewcCode, SUM(total_uom) AS totalTonne, SUM(co2_saving) AS totalCO2 
FROM `enviro_figures_upload 
    WHERE customer_site = 'A Customer' 
    AND completion_date BETWEEN '2014-01-01' AND '2015-01-15' 
    AND completion_date != '' 
    AND total_uom != '' 
    AND waste_type != '' 
    GROUP BY waste_type 
    ORDER BY completion_date DESC 

這是輸出

wasteType       ewcCode  totalTonne totalCO2 
Dry Mixed Recycling     20 03 01 20   2 
Confidential Paper Shred service 20 01 01 75   0.84 

然而,Symfony2的應用程序中返回結果只有一個:

wasteType       ewcCode  totalTonne totalCO2 
Confidential Paper Shred service 20 01 01 75   0.84 

我還用裏面的Symfony2但返回原生SQL查詢相同的結果。即使刪除任何要搜索的參數也會返回一條記錄(即使使用此方法,它應顯示100條記錄)。

這裏是有問題表的實體文件:

<?php 

namespace CWWA\CoreBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

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

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="account_number", type="integer", nullable=false) 
    */ 
    private $accountNumber; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="customer_reference", type="integer", nullable=false) 
    */ 
    private $customerReference; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="billing_customer", type="string", length=500, nullable=false) 
    */ 
    private $billingCustomer; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="division", type="string", length=500, nullable=false) 
    */ 
    private $division; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="customer_site", type="string", length=500, nullable=false) 
    */ 
    private $customerSite; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="town", type="string", length=500, nullable=false) 
    */ 
    private $town; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="postcode", type="string", length=12, nullable=false) 
    */ 
    private $postcode; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="job_number", type="integer", nullable=false) 
    */ 
    private $jobNumber; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="job_status", type="string", length=50, nullable=false) 
    */ 
    private $jobStatus; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="job_type", type="string", length=50, nullable=false) 
    */ 
    private $jobType; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="completion_date", type="string", length=45, nullable=false) 
    */ 
    private $completionDate; 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



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

    /** 
    * Set accountNumber 
    * 
    * @param integer $accountNumber 
    * @return EnviroFiguresUpload 
    */ 
    public function setAccountNumber($accountNumber) 
    { 
     $this->accountNumber = $accountNumber; 

     return $this; 
    } 

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

    /** 
    * Set customerReference 
    * 
    * @param integer $customerReference 
    * @return EnviroFiguresUpload 
    */ 
    public function setCustomerReference($customerReference) 
    { 
     $this->customerReference = $customerReference; 

     return $this; 
    } 

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

    /** 
    * Set billingCustomer 
    * 
    * @param string $billingCustomer 
    * @return EnviroFiguresUpload 
    */ 
    public function setBillingCustomer($billingCustomer) 
    { 
     $this->billingCustomer = $billingCustomer; 

     return $this; 
    } 

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

    /** 
    * Set division 
    * 
    * @param string $division 
    * @return EnviroFiguresUpload 
    */ 
    public function setDivision($division) 
    { 
     $this->division = $division; 

     return $this; 
    } 

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

    /** 
    * Set customerSite 
    * 
    * @param string $customerSite 
    * @return EnviroFiguresUpload 
    */ 
    public function setCustomerSite($customerSite) 
    { 
     $this->customerSite = $customerSite; 

     return $this; 
    } 

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

    /** 
    * Set town 
    * 
    * @param string $town 
    * @return EnviroFiguresUpload 
    */ 
    public function setTown($town) 
    { 
     $this->town = $town; 

     return $this; 
    } 

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

    /** 
    * Set postcode 
    * 
    * @param string $postcode 
    * @return EnviroFiguresUpload 
    */ 
    public function setPostcode($postcode) 
    { 
     $this->postcode = $postcode; 

     return $this; 
    } 

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

    /** 
    * Set jobNumber 
    * 
    * @param integer $jobNumber 
    * @return EnviroFiguresUpload 
    */ 
    public function setJobNumber($jobNumber) 
    { 
     $this->jobNumber = $jobNumber; 

     return $this; 
    } 

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

    /** 
    * Set jobStatus 
    * 
    * @param string $jobStatus 
    * @return EnviroFiguresUpload 
    */ 
    public function setJobStatus($jobStatus) 
    { 
     $this->jobStatus = $jobStatus; 

     return $this; 
    } 

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

    /** 
    * Set jobType 
    * 
    * @param string $jobType 
    * @return EnviroFiguresUpload 
    */ 
    public function setJobType($jobType) 
    { 
     $this->jobType = $jobType; 

     return $this; 
    } 

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

    /** 
    * Set completionDate 
    * 
    * @param string $completionDate 
    * @return EnviroFiguresUpload 
    */ 
    public function setCompletionDate($completionDate) 
    { 
     $this->completionDate = $completionDate; 

     return $this; 
    } 

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

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

     return $this; 
    } 

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

    /** 
    * Set wasteType 
    * 
    * @param string $wasteType 
    * @return EnviroFiguresUpload 
    */ 
    public function setWasteType($wasteType) 
    { 
     $this->wasteType = $wasteType; 

     return $this; 
    } 

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

    /** 
    * Set ewcCode 
    * 
    * @param string $ewcCode 
    * @return EnviroFiguresUpload 
    */ 
    public function setEwcCode($ewcCode) 
    { 
     $this->ewcCode = $ewcCode; 

     return $this; 
    } 

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

    /** 
    * Set container 
    * 
    * @param string $container 
    * @return EnviroFiguresUpload 
    */ 
    public function setContainer($container) 
    { 
     $this->container = $container; 

     return $this; 
    } 

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

    /** 
    * Set quantity 
    * 
    * @param integer $quantity 
    * @return EnviroFiguresUpload 
    */ 
    public function setQuantity($quantity) 
    { 
     $this->quantity = $quantity; 

     return $this; 
    } 

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

    /** 
    * Set totalUom 
    * 
    * @param string $totalUom 
    * @return EnviroFiguresUpload 
    */ 
    public function setTotalUom($totalUom) 
    { 
     $this->totalUom = $totalUom; 

     return $this; 
    } 

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

    /** 
    * Set co2Saving 
    * 
    * @param string $co2Saving 
    * @return EnviroFiguresUpload 
    */ 
    public function setCo2Saving($co2Saving) 
    { 
     $this->co2Saving = $co2Saving; 

     return $this; 
    } 

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

    /** 
    * Set totalResource 
    * 
    * @param string $totalResource 
    * @return EnviroFiguresUpload 
    */ 
    public function setTotalResource($totalResource) 
    { 
     $this->totalResource = $totalResource; 

     return $this; 
    } 

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

    /** 
    * Set totalCo2Saving 
    * 
    * @param string $totalCo2Saving 
    * @return EnviroFiguresUpload 
    */ 
    public function setTotalCo2Saving($totalCo2Saving) 
    { 
     $this->totalCo2Saving = $totalCo2Saving; 

     return $this; 
    } 

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

    /** 
    * Set recoveryRate 
    * 
    * @param string $recoveryRate 
    * @return EnviroFiguresUpload 
    */ 
    public function setRecoveryRate($recoveryRate) 
    { 
     $this->recoveryRate = $recoveryRate; 

     return $this; 
    } 

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

    /** 
    * Set disposalMethod 
    * 
    * @param string $disposalMethod 
    * @return EnviroFiguresUpload 
    */ 
    public function setDisposalMethod($disposalMethod) 
    { 
     $this->disposalMethod = $disposalMethod; 

     return $this; 
    } 

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

    /** 
    * Set wasteHierarchy 
    * 
    * @param string $wasteHierarchy 
    * @return EnviroFiguresUpload 
    */ 
    public function setWasteHierarchy($wasteHierarchy) 
    { 
     $this->wasteHierarchy = $wasteHierarchy; 

     return $this; 
    } 

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

    /** 
    * Set customerOrderDate 
    * 
    * @param string $customerOrderDate 
    * @return EnviroFiguresUpload 
    */ 
    public function setCustomerOrderDate($customerOrderDate) 
    { 
     $this->customerOrderDate = $customerOrderDate; 

     return $this; 
    } 

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

    /** 
    * Set jobNotes 
    * 
    * @param string $jobNotes 
* @return EnviroFiguresUpload 
*/ 
public function setJobNotes($jobNotes) 
{ 
    $this->jobNotes = $jobNotes; 

    return $this; 
} 

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

/** 
* Set customerRefPo 
* 
* @param string $customerRefPo 
* @return EnviroFiguresUpload 
*/ 
public function setCustomerRefPo($customerRefPo) 
{ 
    $this->customerRefPo = $customerRefPo; 

    return $this; 
} 

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

/** 
* Set uploaded 
* 
* @param \Date $uploaded 
* @return EnviroFiguresUpload 
*/ 
public function setUploaded($uploaded) 
{ 
    $this->uploaded = $uploaded; 

    return $this; 
} 

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

/** 
* Set processed 
* 
* @param boolean $processed 
* @return EnviroFiguresUpload 
*/ 
public function setProcessed($processed) 
{ 
    $this->processed = $processed; 

    return $this; 
} 

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

/** 
* Set deleted 
* 
* @param boolean $deleted 
* @return EnviroFiguresUpload 
*/ 
public function setDeleted($deleted) 
{ 
    $this->deleted = $deleted; 

    return $this; 
} 

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

}

從我迄今所做的,它似乎是一個Symfony2的問題,我可以檢索數據從MySQL很容易。它在這裏不起作用。

編輯
我注意到,與表的整理,所有字段都設置爲latin1_swedish_ci。我已將此更改爲utf8_unicode_ci,但這沒有什麼區別。我也刪除了查詢中的空白支票,這仍然只返回一條記錄。

EDIT 2
我用EchoSQLLogger,這是內部的Symfony2這個特定的查詢輸出:

SELECT e0_.waste_type AS waste_type0, e0_.ewc_code AS ewc_code1, SUM(e0_.total_uom) AS sclr2, SUM(e0_.co2_saving) AS sclr3 FROM enviro_figures_upload e0_ WHERE e0_.customer_site = ? AND e0_.completion_date BETWEEN ? AND ? AND e0_.completion_date <> ? AND e0_.total_uom <> ? AND e0_.waste_type <> ? GROUP BY e0_.waste_type ORDER BY e0_.completion_date DESC 
array(6) { 
    [0]=> 
    string(25) "CBS - Arena Park Coventry" 
    [1]=> 
    string(10) "2014-01-16" 
    [2]=> 
    string(10) "2015-01-16" 
    [3]=> 
    string(0) "" 
    [4]=> 
    string(0) "" 
    [5]=> 
    string(0) "" 
} 
array(6) { 
    [0]=> 
    int(2) 
    [1]=> 
    int(2) 
    [2]=> 
    int(2) 
    [3]=> 
    int(2) 
    [4]=> 
    int(2) 
    [5]=> 
    int(2) 
} 

回答

0

看來你與空刺痛AND total_uom != ''在自定義查詢在哪裏比較symfony查詢,您正在與AND efu.totalUom != :zero進行比較。

另外group by聲明在symfony查詢中丟失。

+0

我已經添加了那些缺失的位,並沒有區別。我甚至刪除了對空字符串的檢查,並且,它只是返回一個結果。 – mickburkejnr

相關問題