gpt4 book ai didi

mysql - Doctrine2 findOneBy 返回错误结果

转载 作者:行者123 更新时间:2023-11-29 11:29:40 24 4
gpt4 key购买 nike

我正在使用 Symfony2.3 和以下版本的学说组件(composer show -i | grep 学说)开发一个遗留项目:

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

我必须实现一个简单的数据库缓存系统。

我有一个存储表和一个访问它的服务类。表架构如下:

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`)
)

我遇到的一个大问题是,有时当我尝试检索一个值时,我会得到错误的结果。我正在追踪日志文件,我可以看到正在使用正确的键值执行查询,如果我直接在 MySQL 中运行它,我将得到我期望的值,但不是按照教义。

以下是存储服务的相关代码:

/**
* @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;
}

因此,使用调试器(和 mysql 日志),我可以看到一切都很好,直到出现以下行: ->findOneBy(['key' => $key]);

这将使用具有不同键和值的实体进行设置。

我设置了一个小例子:

$storage->set('test', '1111111111111111111');
$storage->set('test2', '22222222222222222222');
var_dump($storage->fetch('test'));
var_dump($storage->fetch('test2'));

返回:

string '1111111111111111111' (length=19)

string '1111111111111111111' (length=19)

表中的内容如下:

enter image description here

这是mysql日志输出:

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

我在doctrine2上做错了什么吗?我正在更新实体而不是删除,因为学说会在删除之前尝试插入。

谢谢!

更新:

这是实体代码

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

最佳答案

您的实体 id 注释中有错误。目前是:

 * @ORM\Column(name="id", type="boolean")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")

但应该是

 * @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")

关于mysql - Doctrine2 findOneBy 返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37624351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com