Last active
November 12, 2018 02:42
-
-
Save jwage/4e3453aa24c64979b15592a457dc0295 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Banks\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
/** | |
* https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html | |
* | |
* @ORM\Entity | |
* @ORM\Table(name="bank") | |
**/ | |
class Banks implements \JsonSerializable | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer", name="id", nullable=false) | |
* @ORM\GeneratedValue(strategy="IDENTITY") | |
*/ | |
protected $id; | |
/** | |
* A Bank could have Many Branches | |
* | |
* @ORM\OneToMany(targetEntity="Branches\Entity\Branches", mappedBy="bank") | |
* | |
*/ | |
protected $branches; | |
/** | |
* @ORM\Column(type="string", nullable=true) | |
*/ | |
protected $name; | |
/** | |
* | |
* @return array|mixed | |
*/ | |
public function jsonSerialize() | |
{ | |
return [ | |
'id' => $this->id, | |
'name' => $this->name, | |
'branches' => $this->getBranches()->map(function(Branches $branch) { | |
return [ | |
'id' => $branch->getId(), | |
'bank_id' => $branch->getBankId(), | |
'name' => $branch->getName(), | |
]; | |
})->toArray(), | |
]; | |
} | |
public function __construct() | |
{ | |
$this->branches = new ArrayCollection(); | |
} | |
public function getBranches(): Collection | |
{ | |
return $this->branches; | |
} | |
// ... Other getter/setters removed | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Branches\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html | |
* | |
* @ORM\Entity | |
* @ORM\Table(name="branches") | |
**/ | |
class Branches implements \JsonSerializable | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer", nullable=false) | |
* @ORM\GeneratedValue(strategy="IDENTITY") | |
*/ | |
protected $id; | |
/** | |
* A Branch has one Bank | |
* | |
* @ORM\ManyToOne(targetEntity="Banks\Entity\Banks", inversedBy="branches") | |
* @ORM\JoinColumn(name="bank_id", referencedColumnName="id") | |
*/ | |
protected $bank; | |
/** | |
* @ORM\Column(type="integer", nullable=false) | |
*/ | |
protected $bank_id; | |
/** | |
* @ORM\Column(type="string", nullable=true) | |
*/ | |
protected $name; | |
/** | |
* | |
* @return array|mixed | |
*/ | |
public function jsonSerialize() | |
{ | |
return [ | |
'id' => $this->id, | |
'bank_id' => $this->bank_id, | |
'name' => $this->name, | |
// getBank returns an object which json_encode can't handle | |
//'bank' => $this->getBank() | |
]; | |
} | |
public function getBank() | |
{ | |
return $this->bank; | |
} | |
// ... Other getter/setters removed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment