Last active
May 22, 2016 20:20
-
-
Save fesor/7e9a14587e90b7539924fe697438f893 to your computer and use it in GitHub Desktop.
Doctrine is Cool
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 | |
| class Comment | |
| { | |
| private $message; | |
| private $author; | |
| private $createdAt; | |
| public function __construct(string $message, User $author) | |
| { | |
| $this->message = $message; | |
| $this->author = $author; | |
| $this->createdAt = new DateTime(); | |
| } | |
| } |
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 | |
| class Post | |
| { | |
| private $id; | |
| private $title; | |
| private $content; | |
| private $comments; | |
| public function __construct(string $title, string $content) | |
| { | |
| $this->title = $title; | |
| $this->content = $content; | |
| $this->createdAt = new DateTime(); | |
| $this->comments = new ArrayCollection(); | |
| } | |
| public function addComment(string $comment, User $author) | |
| { | |
| $this->comments->add(new Comment($comment, $author)); | |
| } | |
| } |
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 | |
| class PostsController extends Controller | |
| { | |
| // .. | |
| /** | |
| * @Config\Method("POST") | |
| * @Config\Route('/posts/{id}/comments') | |
| */ | |
| public function addCommentAction(Request $request, Post $post) | |
| { | |
| $post->addComment($request->get('message'), $this->getUser()); | |
| $this->flushChanges(); | |
| return new Response(null, 201); | |
| } | |
| // .. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment