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; class AccTrans { private $accTransId; private $amount; private $validated = '0'; private $description; private $createdAt; private $updatedAt; private $acc; public function getAccTransId() { return $this->accTransId; } public function setAmount($amount) { $this->amount = $amount; return $this; } public function getAmount() { return $this->amount; } public function setValidated($validated) { $this->validated = $validated; return $this; } public function getValidated() { return $this->validated; } public function setDescription($description) { $this->description = $description; return $this; } public function getDescription() { return $this->description; } public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; return $this; } public function getCreatedAt() { return $this->createdAt; } public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } public function getUpdatedAt() { return $this->updatedAt; } public function setAcc(\Domain\Entity\Acc $acc = null) { $this->acc = $acc; return $this; } 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.