2017-07-28 6 views
0

私は自分のプロジェクトでentitymanagerを使用しています。私は成功裏に私のentitesとそれらの間の関係をデータベースに読み込み/追加することができますが、1つの問題があります。PHP neo4j OGM - 再帰が検出されました

私は2つのノードEmployeeとDocumentを持ち、それらの間にEmployee HAS Documentという関係を持っています。 Emloyeeため

PHPクラス:ドキュメントの

<?php 
namespace App\Models; 

use GraphAware\Neo4j\OGM\Annotations as OGM; 
use \GraphAware\Neo4j\OGM\Common\Collection; 

/** 
* 
* @OGM\Node(label="Employee") 
*/ 
class Employee implements \JsonSerializable { 

    public function __construct() { 
     $this->documents = new Collection(); 
     $this->addresses = new Collection(); 
    } 

    /** 
    * Id 
    * @var int 
    * @OGM\GraphId() 
    */ 
    protected $id; 

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

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

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

    /** 
    * @var Document[]|Collection 
    * 
    * @OGM\Relationship(type="HAS", direction="OUTGOING", collection=true, mappedBy="employees", targetEntity="Document") 
    */  
    protected $documents; 


    /** 
    * @var Address[]|Collection 
    * 
    * @OGM\Relationship(type="HAS", direction="OUTGOING", collection=true, mappedBy="employees", targetEntity="Address") 
    */  
    protected $addresses; 

    public function getaddresses() { 
     return $this->addresses; 
    } 

    public function getdocuments(){ 
     return $this->documents; 
    }  

    public function getname(){ 
     return $this->name; 
    } 

    public function setname($name){ 
     $this->name = $name; 
    } 

    function getlastname() { 
     return $this->lastname; 
    } 
    public function setlastname($lastname){ 
     $this->lastname = $lastname; 
    } 

    function getpersonalidnumber() { 
     return $this->lastname; 
    } 
    public function setpersonalidnumber($personalidnumber){ 
     $this->personalidnumber = $personalidnumber; 
    } 



    public function jsonSerialize() { 
     return [ 
      'id' => $this->id, 
      'name' => $this->name, 
      'lastName' => $this->lastname, 
      'personalidnumber' => $this->personalidnumber, 
      'addresses' =>$this->addresses->toArray(), 
      'documents' =>$this->documents->toArray() 
     ]; 
    } 
} 

PHPクラス:

<?php 

namespace App\Models; 

use GraphAware\Neo4j\OGM\Annotations as OGM; 
use GraphAware\Neo4j\OGM\Common\Collection; 

/** 
* 
* @OGM\Node(label="Document") 
*/ 
class Document implements \JsonSerializable{ 

    public function __construct() { 
     $this->employees = new Collection(); 

     $timezone = new \DateTimeZone('Europe/Ljubljana'); 
     $t = microtime(true); 
     $micro = sprintf("%06d",($t - floor($t)) * 1000000); 
     $dt = new \DateTime(date('Y-m-d H:i:s.'.$micro, $t)); 
     $dt->setTimezone($timezone); 
     $this->crdate = $dt->format('YmdHis.u'); 
     $this->validfrom = $this->crdate; 
    } 

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

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

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

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


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

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

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

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

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

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


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

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

    /** 
    * @var DocEmployees[]|Collection 
    * 
    * @OGM\Relationship(type="HAS", direction="INCOMING", collection=true, mappedBy="documents", targetEntity="Employee") 
    */ 
    protected $employees; 


    public function getemployees() { 
     return $this->employees; 
    } 

    public function getid() { 
     return $this->id; 
    } 

    public function getname() { 
     return $this->name; 
    } 
    public function setname($name) { 
     $this->name = $name; 
    } 

    public function getuniquename() { 
     return $this->uniquename; 
    } 
    public function setuniquename($uniquename) { 
     $this->uniquename = $uniquename; 
    } 

    public function getpath() { 
     return $this->path; 
    } 
    public function setpath($path) { 
     $this->path = $path; 
    } 

    public function getext() { 
     return $this->ext; 
    } 
    public function setext($ext) { 
     $this->ext = $ext; 
    } 

    public function getdocumentid() { 
     return $this->documentid; 
    } 
    public function setdocumentid($documentid) { 
     $this->documentid = $documentid; 
    }  

    public function getvalidfrom() { 
     return $this->validfrom; 
    } 

    public function setvalidfrom($validfrom) { 
     $this->validfrom = $validfrom; 
    } 

    public function getvalidto() { 
     return $this->validto; 
    } 

    public function setvalidto($validto) { 
     $this->validto = $validto; 
    }  

    public function getlanguage() { 
     return $this->language; 
    } 
    public function setlanguage($language) { 
     $this->language = $language; 
    } 

    public function getcrdate() { 
     return $this->crdate; 
    } 
    public function setcrdate($crdate) { 
     $this->crdate = $crdate; 
    } 

    public function gettype() { 
     return $this->type; 
    } 
    public function settype($type) { 
     $this->type = $type; 
    } 

    public function getdescription() { 
     return $this->description; 
    } 
    public function setdescription($description) { 
     $this->description = $description; 
    } 

    public function getfullname(){ 
     return $this->path.$this->uniquename; 
    } 

    public function jsonSerialize() { 
     return [ 
      'id' => $this->id, 
      'name' => $this->name, 
      'uniquename' => $this->uniquename, 
      'path' => $this->path, 
      'ext' => $this->ext, 
      'validfrom' => $this->validfrom, 
      'validto' => $this->validto, 
      'language' => $this->language, 
      'crdate' => $this->crdate, 
      'type' => $this->type, 
      'description' => $this->description, 
      'employees' => $this->employees->toArray() 
     ]; 
    } 
} 

その後、私は、RESTのAPI のためのスリムなフレームワークを使用する2つのAPIエンドポイントは、基本的に私はその2つのメソッドを持っていますデータを返します。ドキュメントのための

public function getAllEmployees(){ 
    $this->logger->info(__CLASS__.':'.__FUNCTION__); 


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

    return $employees; 
} 

:従業員のための

私はDocumentクラスにライン、その後

  'employees' => $this->employees->toArray() 

のコメントを解除するとき

public function getAllDocuments(){ 
    $this->logger->info(__CLASS__.':'.__METHOD__); 

    $documentRepository = $this->dbentity->getRepository(Document::class); 
    $documents = $documentRepository->findAll(); 

    return $documents; 
} 

だから私の問題は私が手でありますRuntime error再帰が検出されました

この行はすべて問題なく動作します。

誰かが私に何か迷っているのを助けることができますか?

答えて

0

ドキュメントのjsonSerialize()にすべての従業員が含まれているため、エラーが発生します。従業員のjsonSerialize()にはすべての文書が含まれているので、決して終わらない文書の無限のネスティングを作成します。 APIレスポンスの従業員レコード内にドキュメントを含める場合は、すべてのドキュメントのシリアライズをスキップする必要があります。

関連する問題