Symfony注意:unserialize():40字节的偏移36处的错误,从Mysql移动到PostgreSQL

时间:2017-02-07 15:21:28

标签: php mysql postgresql symfony

我在内部打电话给api时遇到了问题。 日志说:

[2017-02-07 16:04:39] doctrine.DEBUG: SELECT h0_.id AS id_0, h0_.hash AS hash_1, h0_.request AS request_2, h0_.options AS options_3, h0_.serialized_response_body AS serialized_response_body_4, h0_.response AS response_5, h0_.sent_at AS sent_at_6 FROM http_request h0_ WHERE h0_.hash = ? ["dd3e36a5a38618974cae2b45f9cb3a67"] []
[2017-02-07 16:04:39] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: unserialize(): Error at offset 36 of 40 bytes" at /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ObjectType.php line 57 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\ContextErrorException(code: 0): Notice: unserialize(): Error at offset 36 of 40 bytes at /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ObjectType.php:57)"} []

我有点丢失>。<

编辑:

在我的控制器中,我提交了一个包含多个信息的表单,以便完成此过程。

 /**
 * @Rest\View(serializerGroups={"Review"})
 * @Security("is_granted('ROLE_REVIEW_CREATE')")
 */
public function postAction(Request $request)
{
    if ($this->getUser()->getBalance()->getTokens() == 0) {
        throw new AccessDeniedException('The Review can not be created because you lack of tokens');
    }

    $review = new Review();
    $review->setCreatedBy($this->getUser());

    $reviewForm = $this->createForm(ReviewType::class, $review, [
        'csrf_protection' => false,
    ]);
    $reviewForm->submit($request->request->all());
    if ($reviewForm->isSubmitted() && $reviewForm->isValid()) {
        // If the user is in an organization, takes the organization analyzer environment, otherwise takes the user one.
        $analyzerEnvironment = $this->getUser()->getCompany() ? $this->getUser()->getCompany()->getAnalyzerEnvironment() : $this->getUser()->getAnalyzerEnvironment();

        $crawlingFlow = $this->get('app.manager.crawling_flow')->getCrawlingFlow();

        $this->get('app.manager.review')->process(
            $review,
            $crawlingFlow,
            $analyzerEnvironment
        );

        $reviewEncrypted = new ReviewEncrypted();
        $reviewEncrypted->setReliability($review->getReliability());
        $reviewEncrypted->setData($this->get('app.manager.review')->encrypt($review));
        $reviewEncrypted->setCreatedBy($this->getUser());
        $this->getDoctrine()->getManager()->persist($reviewEncrypted);
        $this->getDoctrine()->getManager()->flush();
        $review->setId($reviewEncrypted->getId());

        if ($this->getUser()->getBalance()->getTokens() > 0) {
            $this->getUser()->getBalance()->setTokens($this->getUser()->getBalance()->getTokens() - 1);
            $this->getDoctrine()->getManager()->persist($this->getUser());
            $this->getDoctrine()->getManager()->flush();
        }

        return $review;
    }

    return $reviewForm;
}

致电时:

$crawlingFlow = $this->get('app.manager.crawling_flow')->getCrawlingFlow();

    $this->get('app.manager.review')->process(
        $review,
        $crawlingFlow,
        $analyzerEnvironment
    );

调用了几个服务,它们发送请求,执行诸如存储在DB等中的操作......

问题是,当我更改数据库(从mysql到postgresql)时,有什么我忘记做的事情,还是其他什么?

unserialize()错误可能出现在哪里?为什么?

感谢您的帮助:p

编辑2:

这里的错误来自log" vendor / doctrine / dbal / lib / Doctrine / DBAL / Types / ObjectType.php第57行" :

/**
 * {@inheritdoc}
 */
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
    return serialize($value);
}

/**
 * {@inheritdoc}
 */
public function convertToPHPValue($value, AbstractPlatform $platform)
{
    if ($value === null) {
        return null;
    }

    $value = (is_resource($value)) ? stream_get_contents($value) : $value;
    $val = unserialize($value);
    if ($val === false && $value !== 'b:0;') {
        throw ConversionException::conversionFailed($value, $this->getName());
    }

    return $val;
}

1 个答案:

答案 0 :(得分:0)

MySQL的text和PostgreSQL之间存在一个重要的区别,可能是你的情况。 PostgreSQL在文本字段中不允许\ 0。所以,在准备阶段pg驱动程序首先剪切字符串\ 0。

不幸的是,serialize在内部使用\ 0符号。

class A {
  private $a = 'a';
  private $b = 'b';
}

$a = new A();

var_export(serialize($a));

将显示 'O:1:"A":2:{s:4:"' . "\0" . 'A' . "\0" . 'a";s:1:"a";s:4:"' . "\0" . 'A' . "\0" . 'b";s:1:"b";}'

这个sting的MySQL插入工作正常: INSERT INTO test1 VALUES ('O:1:\"A\":2:{s:4:\"\0A\0a\";s:1:\"a\";s:4:\"\0A\0b\";s:1:\"b\";}')

但是Pg司机默默切断字符串并执行
LOG: execute pdo_stmt_00000001: INSERT INTO test1 VALUES ($1)
DETAIL: parameters: $1 = 'O:1:"A":2:{s:4:"'
INSERT INTO test1 VALUES ('O:1:"A":2:{s:4:"')准备模拟。

正如您所看到的 - 结果是剪切字符串,反序列化无法恢复它。

解决方案:对Postgresql使用bytea数据类型或添加某种序列化字符串的编码/解码。

UPD:Doctrine http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#object

中约object个数据类型