Difference between $query->getResult() and $repository->findOneBy(['uuid' => $uuid])
slavkor opened this issue · 1 comments
Hello,
Why am i getting different JSON for the same class when returning data by $query->getResult() or $repository->findOneBy(['uuid' => $uuid]).
Here is the code.
The model:
`
/**
*
* @OGM\Node(label="Employee")
*/
class EmployeeAddresses extends BaseEmployee implements \JsonSerializable {
public function __construct() {
parent::__construct();
$this->addresslist = new Collection();
}
/**
* @var Address[]|Collection
*
* @OGM\Relationship(type="LIVES", direction="OUTGOING", collection=true, mappedBy="", targetEntity="Address")
*/
protected $addresslist;
public function getAddresslist(): array {
return $this->addresslist;
}
public function setAddresslist(array $addresslist) {
$this->addresslist = $addresslist;
}
public function jsonSerialize() {
$ret = parent::jsonSerialize();
if(is_array($this->addresslist))
{
$ret['addresses'] = $this->addresslist;
}
else if ($this->addresslist->count() > 0) {
$ret['addresses'] = $this->addresslist->toArray();
} else {
$ret['addresses'] = NULL;
}
return $ret;
}
}
`
Repository with $query->getResult()
`
public function getAddresses($uuid) {
$query = $this->dbentity->createQuery("match(e:Employee{uuid:{uuid}})-[r:LIVES]-(a:Address{deleted:0}) return distinct e");
$query->addEntityMapping("e", EmployeeAddresses::class);
$query->setParameter("uuid", $uuid);
$emp = $query->getResult();
return $emp;
}
`
Repository with $repository->findOneBy(['uuid' => $uuid])
`
public function getAddresses($uuid) {
$rep = $this->dbentity->getRepository(EmployeeAddresses::class);
$emp = $repository->findOneBy(['uuid' => $uuid]);
return $emp;
}
`
For the data returned by $query->getResult() when they are serialized to JSON the output is an array of EmployeeAddresses objects like so:
[ { "id": 1516, "uuid": "de1c80a7-a02c-4ee3-bdf9-9df01dcc4f1f", "active": null, "crdate": null, "mfdate": null, "deleted": null, "name": "Slavko", "lastname": "Rihtari\u010d", "nickname": null, "birthday": "19801227000000", "birthplace": "Ptuj", "emso": "2712980500427", "taxnumber": null, "personalidnumber": null, "iban": null, "position": "", "addresses": [ { "id": 1664, "uuid": "884292fc-88bc-4321-9978-9bc31a6c0168", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "a", "line2": "a", "city": "a", "state": "a", "zip": "aa", "country": "a", "type": null }, { "id": 1653, "uuid": "9f936adc-e669-44ad-8dec-5f4b47a00aaa", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "gaaaaa", "line2": "g", "city": "g", "state": "g", "zip": "g", "country": "g", "type": null }, { "id": 1649, "uuid": "9469c8ce-a058-441c-9cfe-1f31a4f9764c", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "a", "line2": "d", "city": "d", "state": "f", "zip": "f", "country": "f", "type": null }, { "id": 1645, "uuid": "cb89eb61-33bb-4984-b81f-de9b6cf7a341", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "d", "line2": "d", "city": "d", "state": "d", "zip": "d", "country": "d", "type": null }, { "id": 1641, "uuid": "6ca1b364-9e4c-47dc-891b-a9eaa770a846", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "b", "line2": "asd", "city": "b", "state": "b", "zip": "b", "country": "b", "type": null }, { "id": 1633, "uuid": "cbe186cc-ad4c-4ccd-a18c-1af0731493ca", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "aaa", "line2": "a", "city": "a", "state": "a", "zip": "a", "country": "a", "type": null } ] } ]
For the data returned by $repository->findOneBy(['uuid' => $uuid]) when they are serialized to JSON the output is JSON for EmployeeAddresses object like so:
{ "id": 1516, "uuid": "de1c80a7-a02c-4ee3-bdf9-9df01dcc4f1f", "active": null, "crdate": null, "mfdate": null, "deleted": null, "name": "Slavko", "lastname": "Rihtari\u010d", "nickname": null, "birthday": "19801227000000", "birthplace": "Ptuj", "emso": "2712980500427", "taxnumber": null, "personalidnumber": null, "iban": null, "position": "", "addresses": [ { "id": 1664, "uuid": "884292fc-88bc-4321-9978-9bc31a6c0168", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "a", "line2": "a", "city": "a", "state": "a", "zip": "aa", "country": "a", "type": null }, { "id": 1653, "uuid": "9f936adc-e669-44ad-8dec-5f4b47a00aaa", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "gaaaaa", "line2": "g", "city": "g", "state": "g", "zip": "g", "country": "g", "type": null }, { "id": 1649, "uuid": "9469c8ce-a058-441c-9cfe-1f31a4f9764c", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "a", "line2": "d", "city": "d", "state": "f", "zip": "f", "country": "f", "type": null }, { "id": 1645, "uuid": "cb89eb61-33bb-4984-b81f-de9b6cf7a341", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "d", "line2": "d", "city": "d", "state": "d", "zip": "d", "country": "d", "type": null }, { "id": 1641, "uuid": "6ca1b364-9e4c-47dc-891b-a9eaa770a846", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "b", "line2": "asd", "city": "b", "state": "b", "zip": "b", "country": "b", "type": null }, { "id": 1633, "uuid": "cbe186cc-ad4c-4ccd-a18c-1af0731493ca", "active": 0, "crdate": null, "mfdate": null, "deleted": 0, "line1": "aaa", "line2": "a", "city": "a", "state": "a", "zip": "a", "country": "a", "type": null } ] }
Why so? How can i get the same JSON output in both cases?
With $result nothing prevents the query to return multiple results found, while findOneBy is assuming under the hood that only one result will be returned.
Just do return $emp[0]
if you are sure only one result will be returned, no magic.