Doctrine / MongoDB映射Hash始终返回null

时间:2017-08-22 14:35:56

标签: mongodb symfony doctrine

我有一个像这样的mongo文档:

{
    "_id" : ObjectId("5995ba75a48bb0217c5fabe7"),
    "type" : "staff",
    "firstname" : "Thomas",
    "lastname" : "Dupont",
    "email" : "test.test@gmail.com",
    "password" : "$P$BtCPQaU7mcOqtT7Bmj4h4YUpcy/ont1",
    "role" : [ 
        "ADMIN"
    ],
    "apikey" : "4a6615e073f0267f1a6b258ed1750526",
    "job" : "dev",
    "phoneNumber" : "0651193342",
    "connectionFailed" : {
        "number" : 1,
        "time" : 1503411126
    }
}

我的实体看起来像这样:

<?php
// src/AppBundle/Document/User.php

namespace ApiBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use ApiBundle\Document\{Staff, Customer};

/**
 * @MongoDB\Document
 * @MongoDB\InheritanceType("SINGLE_COLLECTION")
 * @MongoDB\DiscriminatorField("type")
 * @MongoDB\DiscriminatorMap({"staff"="Staff", "customer"="Customer"})
 */
class User
{
    /**
     * @MongoDB\Id(strategy="auto")
     */
    protected $id;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $firstname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $lastname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $email;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $password;

    /**
     * @MongoDB\Field(type="collection")
     */
    protected $role = [];

    /**
     * @MongoDB\Field(type="string")
     */
    protected $apikey;

    /**
     * @MongoDB\Hash
     */
    protected $connectionFailed;

    /**
     * Set type
     *
     * @param string $type
     * @return $this
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * Get type
     *
     * @return string $type
     */
    public function getType()
    {
        return get_called_class();
    }

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

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return $this
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
        return $this;
    }

    /**
     * Get firstname
     *
     * @return string $firstname
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set lastname
     *
     * @param string $lastname
     * @return $this
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
        return $this;
    }

    /**
     * Get lastname
     *
     * @return string $lastname
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return $this
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
     * Get email
     *
     * @return string $email
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return $this
     */
    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    /**
     * Get password
     *
     * @return string $password
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set role
     *
     * @param collection $role
     * @return $this
     */
    public function setRole($role)
    {
        $this->role = $role;
        return $this;
    }

    /**
     * Get role
     *
     * @return collection $role
     */
    public function getRole()
    {
        return $this->role;
    }

    public function toArray() {
        $tmp = [];
        foreach($this as $key => $value) {
            $tmp[$key] = $value;
        }
        return $tmp;
    }

    /**
     * Set apikey
     *
     * @return $this
     */
    public function setApikey()
    {
        $this->apikey = md5(uniqid());
        return $this;
    }

    /**
     * Get apikey
     *
     * @return string $apikey
     */
    public function getApikey()
    {
        return $this->apikey;
    }

    /**
     * Set connectionFailed
     *
     * @param hash $connectionFailed
     * @return $this
     */
    public function setConnectionFailed($connectionFailed)
    {
        $this->connectionFailed = $connectionFailed;
        return $this;
    }

    /**
     * Get connectionFailed
     *
     * @return hash $connectionFailed
     */
    public function getConnectionFailed()
    {
        return $this->connectionFailed;
    }
}

我尝试检索用户数据:

$user = $this->dm
            ->getRepository('ApiBundle:User')
            ->findOneBy(['email' => $email]);
return $user->toArray();

答案是:

  

array(8){[“id”] =&gt; string(24)“5995ba75a48bb0217c5fabe7”
  [ “名字”] =&GT; string(6)“Thomas”[“lastname”] =&gt;串(6)   “Dupont”[“email”] =&gt; string(25)“test.test@gmail.com”
  [ “密码”] =&GT; string(34)“$ P $ BtCPQaU7mcOqtT7Bmj4h4YUpcy / ont1”
  [ “角色”] =&GT; array(1){       [0] =&GT;       string(5)“ADMIN”} [“apikey”] =&gt; string(32)“4a6615e073f0267f1a6b258ed1750526”[“connectionFailed”] =&gt; NULL}

我做了:

php bin/console cache:clear
rm -rf app/cache/

但是connectionFailed总是返回null 配置是:

PHP 7.1 Mongo v3.4.2 alcaeus / mongo-php-adapter:1.1 doctrine / doctrine-bundle:1.6 doctrine / mongodb-odm:1.1 doctrine / mongodb-odm-bundle:3.3 Symfony:3.3

这是我的问题还是不稳定问题?

提前致谢

0 个答案:

没有答案