dddshelf / ddd-in-php-book-issues

Leave your comments, improvements or book mistakes as an issue! Thanks ❤️
https://leanpub.com/ddd-in-php
28 stars 2 forks source link

SqlPostRepository - how it can update existing post? #69

Closed maxgu closed 7 years ago

maxgu commented 7 years ago

on page 219 your code:

class SqlPostRepository implements PostRepository
{
  // ...
  public function save(Post $aPost)
  {
    $sql = 'INSERT INTO posts (id, body, created_at) VALUES (:id, :body, :created_at)';
    $this->execute($sql, [
      'id' => $aPost->id()->id(),
      'body' => $aPost->body()->content(),
      'created_at' => $aPost->createdAt()->format(self::DATE_FORMAT)
    ]);
  }
  //...
}

What if $aPost already exists? How SqlPostRepository must handle this and execute UPDATE instead INSERT?

carlosbuenosvinos commented 7 years ago

Thanks! I have added a comment in the code and a paragraph explaining in more detail such approach.