2015-09-02 16 views
6

Używam Doctrine Orm w projekcie Silex, nad którym obecnie pracuję. Mój problem polega na tym, że jednostka transakcyjna mojego konta ma czasami ujemne podwojenie w polu "kwota". To jest w porządku, jednostka oszczędza. Problem polega na tym, że przy użyciu findOneBy zapytanie kwerendy zamienia wartość zmiennoprzecinkową na ciąg i dlatego go nie znajduje.Doctrine ORM 2 Wybierz On Negative Numbers

My Podmiot:

<?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; 
    } 
} 

My Podmiot zapisać kod:

$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(); 

Zapytanie generowane przez Nauki (od doktryny kłody):

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"}} [] 

Teraz, gdy zgłoszę findOneBy używa dokładnie tych samych danych, zapytanie generowane przez Doctrine to:

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"]} [] 

Jak widać drugi parametr jest teraz konwertowany na ciąg ("-0.300000" zamiast -0.300000), a wynik nie zostanie znaleziony. Czego tu mi brakuje?

Dzięki za poświęcony czas.

Odpowiedz

0

Znaleziono odpowiedź - sztuczka polegała na użyciu dziesiętnych typów danych zamiast Float. Mam nadzieję, że to pomoże komuś w przyszłości.