php-neo4j-ogm EntityManager GetRepository-> FindAll()返回空对象

时间:2017-07-11 12:37:12

标签: php neo4j neo4j-php-ogm

我正在努力从neo4j数据库中读取数据。我使用neo4j-php-ogm库中提供的entitymanager。

        $employeesRepository = $this->entityManager ->getRepository(Employee::class);
        $employees = $employeesRepository->findAll();
        return $employees;

我以json格式返回,输出为:[{},{},{}]

这是我的员工实体类:

  <?php


use GraphAware\Neo4j\OGM\Annotations as OGM;
/**
 * @OGM\Node(label="Employee")
 */

class Employee{
    /**
     * @OGM\GraphId()
     * @var int
     */
    protected   $id;


    /**
     * @OGM\Property(type="string")
     * @var string
     */
    protected   $last_name;


    /**
     * @OGM\Property(type="string")
     * @var string
     * 
     */
    protected   $first_name;

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

    /**
    * @return string
    */
    public function getlast_name(){
        return $this->last_name;
    }

    /**
    * @param string last_name
    */    
    public function setlast_name($param){
        $this->last_name = $param;
    }

    /**
    * @return string
    */    
    public function getfirst_name() {
        return $this->first_name;
    }

    /**
    * @param string first_name
    */    
    public function setfirst_name($param) {
        $this->first_name = $param;
    }


}

我错过了什么?

1 个答案:

答案 0 :(得分:1)

这是因为json_encode不知道如何编码stdClass以外的对象。

您现在可以让您的类实现JsonSerializable并指定应该序列化的属性。

我添加了一个测试,展示了如何做到这一点:

https://github.com/graphaware/neo4j-php-ogm/commit/b013c3c2717cb04af0b0c3ab8a770b207d06e5a0

class TestUser implements \JsonSerializable
{
    /**
     * @OGM\GraphId()
     *
     * @var int
     */
    protected $id;

    /**
     * @OGM\Property()
     *
     * @var string
     */
    protected $name;

    public function __construct($name)
    {
        $this->sponsoredChildren = new Collection();
        $this->name = $name;
    }

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

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    public function jsonSerialize()
    {
        return [
            'id' => $this->id,
            'name' => $this->name
        ];
    }


}

与此同时,我将创建一个问题,以便您能够转换为数组而不是从存储库返回的对象。

https://github.com/graphaware/neo4j-php-ogm/issues/148