Teaching ORM 2 Select Negative Numbers

I am using Doctrine Orm in the Silex project that I am currently working on. My problem is that my transactional transaction object sometimes has a negative doubles in the sum field. This is normal, the object saves. The problem is that when using findOneBy, the select query turns the float value into a string and therefore does not find it.

My essence:

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

The save code for my object is:

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

The request created by Doctrine (from 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"}} [] 

Now, when I call findOneBy using exactly the same data, the query generated by Doctrine is as follows:

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

As you can see, the second parameter is now converted to a string ("-0.300000" instead of -0.300000), and the result was not found. What am I missing here?

Thank you for your time.

+6
source share
1 answer

Found the answer - the trick used decimal data types instead of Float. Hope this helps someone in the future.

0
source

All Articles