Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active May 22, 2016 20:20
Show Gist options
  • Select an option

  • Save fesor/7e9a14587e90b7539924fe697438f893 to your computer and use it in GitHub Desktop.

Select an option

Save fesor/7e9a14587e90b7539924fe697438f893 to your computer and use it in GitHub Desktop.
Doctrine is Cool
<?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();
}
}
<?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));
}
}
<?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