首页 > 解决方案 > 升级后 FOSUser Bundle Login 无法使用 Ramsey UUID

问题描述

我将 symfony 应用程序从 4.0 更新到 4.4 一切正常,除了登录到 ma 仪表板时,Could not convert database value "11ea3053-6c85-df5a-a..." to Doctrine Type uuid_binary_ordered_time. Expected format: UuidV1
如果我降级ramsey/uuid-doctrine版本一切正常,我会收到此错误。
当前 Symfony 版本4.4

同一用户可以使用带有基本身份验证的 rest API 登录。这是我User.php使用 UUID 的 id 参数。提前致谢。

 /**
 * @var Uuid
 *
 * @ORM\Id
 * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
 */
protected $id;


 /**
 * @return string
 */
public function getId()
{
    return $this->id->getHex();
}

在此处输入图像描述

标签: phpsymfonydoctrine-ormuuid

解决方案


所以我找到了上述问题的解决方案。实际上,问题在于 symfony 序列化和反序列化。一切正常,除了使用会话存储用户数据的管理面板登录覆盖了安全性的序列化和反序列化方法,User Class User.php一切正常。
主要的是,在序列化过程中,我必须将我的 uuid 转换为字符串,而在反序列化过程中,我必须将字符串 uuid 转换回。这是我的User.php实体类中的代码。

 public function serialize()
{
    return serialize(array(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->enabled,
        $this->id->toString(),
        $this->email,
        $this->emailCanonical,
    ));
}

public function unserialize($serialized)
{
    $data = unserialize($serialized);

    if (13 === count($data)) {
        // Unserializing a User object from 1.3.x
        unset($data[4], $data[5], $data[6], $data[9], $data[10]);
        $data = array_values($data);
    } elseif (11 === count($data)) {
        // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
        unset($data[4], $data[7], $data[8]);
        $data = array_values($data);
    }
    $data[5] = UuidV1::fromString($data[5]);
    list(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->enabled,
        $this->id,
        $this->email,
        $this->emailCanonical
        ) = $data;
}

推荐阅读