我的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
,但这并没有什么不同。我还删除了查询中的空白支票,这仍然只返回一条记录。
编辑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 :(得分:0)
您似乎在与自定义查询中的空标记AND total_uom != ''
进行比较,其中您在与AND efu.totalUom != :zero
进行比较的symfony查询中。
symfony查询中也缺少group by
语句。