I am struggling to read data from a neo4j db. i use the entitymanager provided in the neo4j-php-ogm library.
$employeesRepository = $this->entityManager ->getRepository(Employee::class);
$employees = $employeesRepository->findAll();
return $employees;
i return this in json format and the output is: [{},{},{}]
this is my Employee entity class:
<?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;
}
}
what am i missing?
This is because json_encode doesn't know how to encode objects other than stdClass
.
You can for now make your class implement JsonSerializable and specify the properties that should be serialized.
I added a test that show how to do it :
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
];
}
}
In the meantime, I will create an issue so that you would be able to convert to an array rather than to an object what is returned from the repository.