Doctrine ORM 2 选择负数

Posted

技术标签:

【中文标题】Doctrine ORM 2 选择负数【英文标题】:Doctrine ORM 2 Select On Negative Numbers 【发布时间】:2015-09-02 04:54:29 【问题描述】:

我在我目前正在进行的 Silex 项目中使用 Doctrine Orm。我的问题是我的帐户交易实体有时在“金额”字段中有负双打。这很好,实体保存。问题是当使用 findOneBy 时,选择查询会将浮点值转换为字符串,因此找不到它。

我的实体:

<?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)并且找不到结果。我在这里错过了什么?

感谢您的宝贵时间。

【问题讨论】:

【参考方案1】:

找到了答案 - 诀窍是使用 Decimal 数据类型而不是 Float。希望这对将来的人有所帮助。

【讨论】:

以上是关于Doctrine ORM 2 选择负数的主要内容,如果未能解决你的问题,请参考以下文章

Doctrine Orm 2 使用本机 SQL 的递归查询

Symfony2:传递给 Doctrine\ORM\EntityRepository::__construct() 的参数 2 必须是 Doctrine\ORM\Mapping\ClassMetada

zend 框架 2 + 教义 2 安装

Doctrine 2 ORM 级联删除相关实体

Doctrine 2.2 ORM:查找扩展实体

Doctrine 2.2 ORM:从数据库进行逆向工程后抛出 MappingException