APC 不会将属性保存在 fosuserbundle 扩展类中



我在将我的文章实体与用户(扩展 FosUserBundle 类)连接时遇到问题。当我在数据库中查询它时,它工作得很好,但是当我使用 APC 时: $driver = DoctrineCommonCacheApcCache(); $driver->save($key, $queryResult),然后要求php$driver->fetch($key)我只接收核心 FosUserBundle 列(id、email 等)的数据,但额外列的 NULLS 数据。

我有:

mappings: FOSUserBundle: ~

在我的 ORM 默认实体管理器配置中。知道会发生什么吗?

问题是

a) FOS\UserBundle\Model\UserInterface extensions \Serializable

b) FOS\UserBundle\Model\User 实现它。

您需要做的是覆盖

public function serialize()

public function unserialize($serialized)

带有考虑您的其他字段的暗示。

/**
 * Serializes the user.
 *
 * The serialized data have to contain the fields used by the equals method and the username.
 *
 * @return string
 */
public function serialize()
{
    return serialize(array(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->expired,
        $this->locked,
        $this->credentialsExpired,
        $this->enabled,
        $this->id,
        $this->someCustomField,
    ));
}

/**
 * Unserializes the user.
 *
 * @param string $serialized
 */
public function unserialize($serialized)
{
    $data = unserialize($serialized);
    // add a few extra elements in the array to ensure that we have enough keys when unserializing
    // older data which does not include all properties.
    $data = array_merge($data, array_fill(0, 2, null));
    list(
        $this->password,
        $this->salt,
        $this->usernameCanonical,
        $this->username,
        $this->expired,
        $this->locked,
        $this->credentialsExpired,
        $this->enabled,
        $this->id,
        $this->someCustomField
    ) = $data;
}

最新更新