Skip to content

Update Entities to use Doctrine 2.6 types #659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DataFixtures/ORM/PostFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function load(ObjectManager $manager): void
$post->setSummary($this->getRandomPostSummary());
$post->setSlug(Slugger::slugify($post->getTitle()));
$post->setContent($this->getPostContent());
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
$post->setPublishedAt(new \DateTimeImmutable('now - '.$i.'days'));

// Ensure that the first post is written by Jane Doe to simplify tests
// "References" are the way to share objects between fixtures defined
Expand All @@ -65,7 +65,7 @@ public function load(ObjectManager $manager): void
$comment = new Comment();

$comment->setAuthor($this->getReference('john-user'));
$comment->setPublishedAt(new \DateTime('now + '.($i + $j).'seconds'));
$comment->setPublishedAt(new \DateTimeImmutable('now + '.($i + $j).'seconds'));
$comment->setContent($this->getRandomCommentContent());

$post->addComment($comment);
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Comment
private $content;

/**
* @var \DateTime
* @var \DateTimeImmutable
*
* @ORM\Column(type="datetime")
* @ORM\Column(type="datetime_immutable")
* @Assert\DateTime
*/
private $publishedAt;
Expand All @@ -78,7 +78,7 @@ class Comment

public function __construct()
{
$this->publishedAt = new \DateTime();
$this->publishedAt = new \DateTimeImmutable();
}

/**
Expand Down Expand Up @@ -106,12 +106,12 @@ public function setContent(string $content): void
$this->content = $content;
}

public function getPublishedAt(): \DateTime
public function getPublishedAt(): \DateTimeInterface
{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): void
public function setPublishedAt(\DateTimeInterface $publishedAt): void
{
$this->publishedAt = $publishedAt;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class Post
private $content;

/**
* @var \DateTime
* @var \DateTimeImmutable
*
* @ORM\Column(type="datetime")
* @ORM\Column(type="datetime_immutable")
* @Assert\DateTime
*/
private $publishedAt;
Expand Down Expand Up @@ -122,7 +122,7 @@ class Post

public function __construct()
{
$this->publishedAt = new \DateTime();
$this->publishedAt = new \DateTimeImmutable();
$this->comments = new ArrayCollection();
$this->tags = new ArrayCollection();
}
Expand Down Expand Up @@ -162,12 +162,12 @@ public function setContent(string $content): void
$this->content = $content;
}

public function getPublishedAt(): \DateTime
public function getPublishedAt(): \DateTimeInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be \DateTimeImmutable.

{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): void
public function setPublishedAt(\DateTimeInterface $publishedAt): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be \DateTimeImmutable to avoid \DateTime instances, same for Comment::setPublishedAt().

Copy link
Member

@yceruto yceruto Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, we can convert here to immutable date time if needed:

if ($publishedAt instanceof \DateTime) {
    $publishedAt = \DateTimeImmutable::createFromMutable($publishedAt);
}

$this->publishedAt = $publishedAt;

Same for Comment::setPublishedAt().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto, What about to create a data transformer for those properties?

Copy link
Member

@yceruto yceruto Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it could be so, but still I'm not sure if it's worth it right now.

{
$this->publishedAt = $publishedAt;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class User implements UserInterface, \Serializable
/**
* @var array
*
* @ORM\Column(type="json_array")
* @ORM\Column(type="json")
*/
private $roles = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function findLatest(int $page = 1): Pagerfanta
WHERE p.publishedAt <= :now
ORDER BY p.publishedAt DESC
')
->setParameter('now', new \DateTime())
->setParameter('now', new \DateTimeImmutable())
;

return $this->createPaginator($query, $page);
Expand Down