我在正在研究的Silex项目中使用Doctrine Orm。我的问题是我的账户交易实体有时在“金额”字段中有负双打。这很好,实体保存。问题是当使用findOneBy时,select查询会将float值转换为字符串,因此无法找到它。
我的实体:
<?php
namespace Domain\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AccTrans
*
* @ORM\Table(name="acc_trans", indexes={@ORM\Index(name="fk_acc_trans_acc1_idx", columns={"acc_id"})})
* @ORM\Entity
*/
class AccTrans
{
/**
* @var integer
*
* @ORM\Column(name="acc_trans_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $accTransId;
/**
* @var float
*
* @ORM\Column(name="amount", type="float", precision=12, scale=6, nullable=true)
*/
private $amount;
/**
* @var integer
*
* @ORM\Column(name="validated", type="smallint", nullable=true)
*/
private $validated = '0';
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @var \Domain\Entity\Acc
*
* @ORM\ManyToOne(targetEntity="Domain\Entity\Acc")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="acc_id", referencedColumnName="acc_id")
* })
*/
private $acc;
/**
* Get accTransId
*
* @return integer
*/
public function getAccTransId()
{
return $this->accTransId;
}
/**
* Set amount
*
* @param float $amount
*
* @return AccTrans
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Get amount
*
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* Set validated
*
* @param integer $validated
*
* @return AccTrans
*/
public function setValidated($validated)
{
$this->validated = $validated;
return $this;
}
/**
* Get validated
*
* @return integer
*/
public function getValidated()
{
return $this->validated;
}
/**
* Set description
*
* @param string $description
*
* @return AccTrans
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return AccTrans
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return AccTrans
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set acc
*
* @param \Domain\Entity\Acc $acc
*
* @return AccTrans
*/
public function setAcc(\Domain\Entity\Acc $acc = null)
{
$this->acc = $acc;
return $this;
}
/**
* Get acc
*
* @return \Domain\Entity\Acc
*/
public function getAcc()
{
return $this->acc;
}
}
我的实体保存代码:
$entity = new \Domain\Entity\AccTrans();
$entity->setAcc($acc_entity);
$entity->setAmount(-(number_format($amount, 6)));
$entity->setValidated(0);
$entity->setDescription("Data Purchase");
$em->persist($entity);
$em->flush();
Doctrine生成的查询(来自Doctrine Logs):
INSERT INTO acc_trans (amount, validated, description, created_at, updated_at, acc_id) VALUES (?, ?, ?, ?, ?, ?) {"params":{"1":"-0.300000","2":1,"3":"Data Purchase","4":null,"5":null,"6":394},"types":{"1":"float","2":"smallint","3":"text","4":"datetime","5":"datetime","6":"integer"}} []
现在,当我使用完全相同的数据调用findOneBy时,Doctrine生成的查询是:
SELECT t0.acc_trans_id AS acc_trans_id_1, t0.amount AS amount_2, t0.validated AS validated_3, t0.description AS description_4, t0.created_at AS created_at_5, t0.updated_at AS updated_at_6, t0.acc_id AS acc_id_7 FROM acc_trans t0 WHERE t0.acc_id = ? AND t0.amount = ? AND t0.validated = ? AND t0.description = ? LIMIT 1 {"params":[394,"-0.300000",1,"Data Purchase"],"types":["integer","float","smallint","text"]} []
如您所见,第二个参数现在转换为字符串(“-0.300000”而不是-0.300000),但未找到结果。我在这里缺少什么?
感谢您的时间。
答案 0 :(得分:0)
找到答案 - 诀窍是使用Decimal数据类型而不是Float。希望这有助于将来。