Created
August 25, 2017 12:41
-
-
Save c0urg3tt3/48ab28910faf3f54f51b75c64d07293d to your computer and use it in GitHub Desktop.
doctrine entities implements JsonSerializable to avoid nesting max depth error
This file contains 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 | |
namespace Acme\ApiBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Todo | |
* | |
* @ORM\Table(name="acme_comments") | |
* @ORM\Entity(repositoryClass="Acme\ApiBundle\Entity\CommentRepository") | |
*/ | |
class Comment implements JsonSerializable | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="content", type="text") | |
*/ | |
private $content; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(name="updated_at", type="datetime") | |
*/ | |
private $updatedAt; | |
/** | |
* Many Comments have One Todo. | |
* @ORM\ManyToOne(targetEntity="Acme\ApiBundle\Entity\Todo", inversedBy="comments") | |
* @ORM\JoinColumn(name="todo_id", referencedColumnName="id") | |
*/ | |
private $todo; | |
// ... GETTER AND SETTER FUNCTIONS ... | |
/** | |
* | |
*/ | |
public function jsonSerialize() | |
{ | |
return [ | |
'id' => $this->getId(), | |
'content' => $this->getContent(), | |
'updatedAt' => $this->getUpdatedAt() | |
]; | |
} | |
} |
This file contains 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 | |
namespace Acme\ApiBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Todo | |
* | |
* @ORM\Table(name="acme_todos") | |
* @ORM\Entity(repositoryClass="Acme\ApiBundle\Entity\TodoRepository") | |
*/ | |
class Todo implements JsonSerializable | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="title", type="string", length=255) | |
*/ | |
private $title; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="content", type="text") | |
*/ | |
private $content; | |
/** | |
* @var boolean | |
* | |
* @ORM\Column(name="is_done", type="boolean") | |
*/ | |
private $isDone; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(name="updated_at", type="datetime") | |
*/ | |
private $updatedAt; | |
/** | |
* One Todo has Many Comments. | |
* @var \ArrayCollection | |
* | |
* @OneToMany(targetEntity="Acme\ApiBundle\Entity\Comment", mappedBy="todo") | |
*/ | |
private $comments; | |
public function __construct() { | |
$this->comments = new ArrayCollection(); | |
} | |
// ... GETTER AND SETTER FUNCTIONS ... | |
/** | |
* | |
*/ | |
public function jsonSerialize() | |
{ | |
return [ | |
'id' => $this->getId(), | |
'title' => $this->getTitle(), | |
'content' => $this->getContent(), | |
'updatedAt' => $this->getUpdatedAt(), | |
'comments' => $this->getComments() | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment