Reputation: 8496
I am working on a legacy project using Symfony2.3 and the following versions of doctrine components (composer show -i | grep doctrine):
doctrine/annotations v1.1.2
doctrine/cache v1.2.0
doctrine/collections dev-master bcb5377
doctrine/common 2.4.x-dev c94d6ff
doctrine/data-fixtures dev-master 8ffac1c
doctrine/dbal 2.3.x-dev 59c310b
doctrine/doctrine-bundle dev-master a41322d
doctrine/doctrine-fixtures-bundle dev-master 3caec48
doctrine/doctrine-migrations-bundle dev-master 1a7f58d
doctrine/inflector dev-master 8b4b3cc
doctrine/lexer dev-master bc0e1f0
doctrine/migrations dev-master e960224
doctrine/orm 2.3.x-dev 66d8b43
I had to implement a simple db cache system.
I have a storage table and a service class to access it. The table schema is as follows:
CREATE TABLE `mlp_api_storage` (
`id` TINYINT(4) NOT NULL AUTO_INCREMENT,
`key` VARCHAR(250) NOT NULL,
`value` TEXT NOT NULL,
`is_serialized` TINYINT(4) NOT NULL,
`created` DATETIME NOT NULL,
`ttl` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `Index 2` (`key`)
)
The big issue I am having with this is one some occasions when I try to retrieve a value I get the wrong result. I am tailing the log file and I can see the query being executed with the correct key value and if I run that directly in MySQL I will get the value I expect but not with doctrine.
Here is the relevant code of the storage service:
/**
* @param string $key
* @param string $value
* @param mixed $ttl
* @return self
*/
public function set($key, $value, $ttl = null)
{
$em = $this->getEntityManager();
$store = $this->fetchEntity($key);
$update = true;
if (!$store) {
$update = false;
$store = new Storage();
}
$store->setKey($key);
if (is_object($value) || is_array($value)) {
$value = serialize($value);
$store->setIsSerialized(true);
} else {
$store->setIsSerialized(false);
}
$store->setValue($value);
$store->setTtl($this->calculateTTL($ttl));
$store->setCreated(new \DateTime());
if ($update) {
$em->merge($store);
} else {
$em->persist($store);
}
$em->flush($store);
return $this;
}
/**
* @param string $key
* @param mixed $default
* @return string|null
*/
public function fetch($key, $default = null)
{
$res = $this->fetchEntity($key);
if ($res) {
if ($this->isValidStorage($res)) {
$value = $res->getValue();
if ($res->getIsSerialized()) {
$value = unserialize($value);
}
return $value;
}
$this->remove($res);
}
if ($default) {
$res = (is_callable($default)) ? $default($this) : $default;
} else {
$res = null;
}
return $res;
}
/**
* @param string $key
* @return Storage
*/
private function fetchEntity($key)
{
/* @var $res Storage */
$res = $this->getEntityManager()
->getRepository(Storage::class)
->findOneBy(['key' => $key]);
if ($res) {
return $res;
}
return null;
}
So using the debugger (and mysql log) I can see that everything is fine up until the line with : ->findOneBy(['key' => $key]);
This will be set with an entity with a different key and value.
I have setup a small example:
$storage->set('test', '1111111111111111111');
$storage->set('test2', '22222222222222222222');
var_dump($storage->fetch('test'));
var_dump($storage->fetch('test2'));
This is returning:
string '1111111111111111111' (length=19)
string '1111111111111111111' (length=19)
Here is what is in the table:
Here is the mysql log output:
66 Query SELECT t0.`key` AS key1, t0.value AS value2, t0.is_serialized AS is_serialized3, t0.created AS created4, t0.ttl AS ttl5, t0.id AS id6 FROM storage t0 WHERE t0.`key` = 'test' LIMIT 1
66 Query SELECT t0.`key` AS key1, t0.value AS value2, t0.is_serialized AS is_serialized3, t0.created AS created4, t0.ttl AS ttl5, t0.id AS id6 FROM storage t0 WHERE t0.`key` = 'test2' LIMIT 1
Am I doing something wrong with doctrine2? I am updating the entity instead of deleting because doctrine will try to insert before deleting.
Thanks!
UPDATE:
Here is the entity code
namespace PitchBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Storage
*
* @ORM\Table(name="storage")
* @ORM\Entity
*/
class Storage
{
/**
* @var string
*
* @ORM\Column(name="`key`", type="string", length=250, nullable=false)
*/
private $key;
/**
* @var string
*
* @ORM\Column(name="value", type="text", nullable=false)
*/
private $value;
/**
* @var boolean
*
* @ORM\Column(name="is_serialized", type="boolean", nullable=false)
*/
private $isSerialized;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* @var integer
*
* @ORM\Column(name="ttl", type="integer", nullable=true)
*/
private $ttl;
/**
* @var boolean
*
* @ORM\Column(name="id", type="boolean")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Set key
*
* @param string $key
* @return Storage
*/
public function setKey($key)
{
$this->key = $key;
return $this;
}
/**
* Get key
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Set value
*
* @param string $value
* @return Storage
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set isSerialized
*
* @param boolean $isSerialized
* @return Storage
*/
public function setIsSerialized($isSerialized)
{
$this->isSerialized = $isSerialized;
return $this;
}
/**
* Get isSerialized
*
* @return boolean
*/
public function getIsSerialized()
{
return $this->isSerialized;
}
/**
* Set created
*
* @param \DateTime $created
* @return Storage
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set ttl
*
* @param integer $ttl
* @return Storage
*/
public function setTtl($ttl)
{
$this->ttl = $ttl;
return $this;
}
/**
* Get ttl
*
* @return integer
*/
public function getTtl()
{
return $this->ttl;
}
/**
* Get id
*
* @return boolean
*/
public function getId()
{
return $this->id;
}
}
Upvotes: 0
Views: 1493
Reputation: 4265
You have an error in your entity id
annotation. Currently it is:
* @ORM\Column(name="id", type="boolean")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
but it should be
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
Upvotes: 1