我需要从读数表中获取最高温度。
我尝试将此SQL查询转换为与createQueryBuilder()
SQL:SELECT temperature_value FROM read_outs WHERE room_id = 3
这在PHPMyAdmin中工作,我得到了结果。现在我只想从所有结果中获取最大值。
我在存储库中创建函数:
public function getMaxTemperatureByRoom($id)
{
$this->createQueryBuilder('read_out')
->select('read_out, MAX(read_out.temperatureValue) AS
max_temperature')
->where('read_out.roomId = :id')
->setParameter('id', $id)
->getQuery()
->getResult();
}
这是我的实体类:
<?php
namespace Smart\SensorBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Smart\RoomBundle\Entity\Room;
/**
* Class ReadOut
* @package Smart\SensorBundle\Entity
*
* @ORM\Table(name="read_outs")
*
@ORM\Entity(repositoryClass=
"Smart\SensorBundle\Repository\ReadOutReposi tory")
*/
class ReadOut
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Sensor
*
* @ORM\ManyToOne(targetEntity="Smart\SensorBundle\Entity\Sensor")
* @ORM\JoinColumn(name="sensor_id", referencedColumnName="id",
onDelete="CASCADE")
*/
private $sensor_id;
/**
* @var \DateTime
*
* @ORM\Column(name="read_date", type="date")
*/
private $readOutDate;
/**
* @var float
*
* @ORM\Column(name="temperature_value", type="float")
*/
private $temperatureValue;
/**
* @var float
*
* @ORM\Column(name="humidity_value", type="float")
*/
private $humidityValue;
/**
* @var Room
*
* @ORM\ManyToOne(targetEntity="Smart\RoomBundle\Entity\Room")
* @ORM\JoinColumn(name="room_id", referencedColumnName="id",
onDelete="CASCADE")
*/
private $roomId;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return Sensor
*/
public function getSensorId(): Sensor
{
return $this->sensor_id;
}
/**
* @param Sensor $sensor_id
*/
public function setSensorId(Sensor $sensor_id): void
{
$this->sensor_id = $sensor_id;
}
/**
* @return \DateTime
*/
public function getReadOutDate(): \DateTime
{
return $this->readOutDate;
}
/**
* @param \DateTime $readOutDate
*/
public function setReadOutDate(\DateTime $readOutDate): void
{
$this->readOutDate = $readOutDate;
}
/**
* @return float
*/
public function getTemperatureValue(): float
{
return $this->temperatureValue;
}
/**
* @param float $temperatureValue
*/
public function setTemperatureValue(float $temperatureValue): void
{
$this->temperatureValue = $temperatureValue;
}
/**
* @return float
*/
public function getHumidityValue(): float
{
return $this->humidityValue;
}
/**
* @param float $humidityValue
*/
public function setHumidityValue(float $humidityValue): void
{
$this->humidityValue = $humidityValue;
}
/**
* @return Room
*/
public function getRoomId(): Room
{
return $this->roomId;
}
/**
* @param Room $roomId
*/
public function setRoomId(Room $roomId): void
{
$this->roomId = $roomId;
}
}
在此我得到NULL:
$result = $this->getDoctrine()->getRepository(ReadOut::class)-
>getMaxTemperatureByRoom(3);
var_dump($result);die;
我做错了什么?
谢谢。
@EDIT
现在我得到这样的东西:
array(2) { [0]=> object(Smart\SensorBundle\Entity\ReadOut)#3563 (6) { ["id":"Smart\SensorBundle\Entity\ReadOut":private]=> int(1) ["sensor_id":"Smart\SensorBundle\Entity\ReadOut":private]=> object(Proxies\__CG__\Smart\SensorBundle\Entity\Sensor)#6017 (14) { ["__initializer__"]=> object(Closure)#3555 (3) { ["static"]=> array(3) { ["entityPersister"]=> object(Doctrine\ORM\Persisters\Entity\BasicEntityPersister)#3529 (13) { ["class":protected]=> object(Doctrine\ORM\Mapping\ClassMetadata)#3524 (40) { ["name"]=> string(32) "Smart\SensorBundle\Entity\Sensor" ["namespace"]=> string(25) "Smart\SensorBundle\Entity" ["rootEntityName"]=> string(32) "Smart\SensorBundle\Entity\Sensor" ["customGeneratorDefinition"]=> NULL ["customRepositoryClassName"]=> string(46) "Smart\SensorBundle\Repository\SensorRepository" ["isMappedSuperclass"]=> bool(false) ["isEmbeddedClass"]=> bool(false) ["parentClasses"]=> array(0) { } ["subClasses"]=> array(0) { } ["embeddedClasses"]=> array(0) { } ["namedQueries"]=> array(0) { } ["namedNativeQueries"]=> array(0) { } ["sqlResultSetMappings"]=> array(0) { } ["identifier"]=> array(1) { [0]=> string(2) "id" }
无最终结果:(。
@ EDIT2
当我使用var_dump
时,dump
有问题,我得到了正确的答复。
感谢您的帮助。
答案 0 :(得分:4)
您忘记了return语句:
public function getMaxTemperatureByRoom($id)
{
return $this->createQueryBuilder('read_out')
->select('read_out, MAX(read_out.temperatureValue) AS max_temperature')
->where('read_out.roomId = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
}