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

Conversation

gabel
Copy link

@gabel gabel commented Sep 26, 2017

Tests will fail due to DateTime conversion of handleRequest() (src/Controller/Admin/BlogController.php:84)

@javiereguiluz
Copy link
Member

What can we do here to move forward?

  • The application is broken after this change. Can we easily fix it?
  • What's the practical advantage of using immutable date types in this application?

Thanks!

@jkufner
Copy link
Contributor

jkufner commented Oct 2, 2017

What's the practical advantage of using immutable date types in this application?

When you pass the same datetime object to two components and one of them adds a day without cloning, the immutable type won't affect the other component, but the mutable will change in both places. It is quite hard to find such bugs.

@javiereguiluz
Copy link
Member

@jkufner yes, that's the theory part. But now, focusing exclusively on practice, which bug does this change fix or what real improvement does it bring to this specific application? Thanks!

@jkufner
Copy link
Contributor

jkufner commented Oct 2, 2017

@javiereguiluz When there is no bug, it does not matter which of the types you use, but you can never be sure there is no bug. Immutable types make code more robust and bugs easier to locate. This change will pay off when some one will modify or build on the application and makes a mistake in the process.

@javiereguiluz
Copy link
Member

@jkufner yes, in general that's true. But in this specific application, which is the benefit of this change? I'm not saying there's none, I just don't see it and that's why I'm asking.

@jkufner
Copy link
Contributor

jkufner commented Oct 2, 2017

@javiereguiluz It is a good practice and a precaution for the future. Something that an example app should highlight. The code itself does the same.

@gabel
Copy link
Author

gabel commented Oct 4, 2017

I assume we cannot move forward here until the underlying libs have fixed the immutable usage. If @javiereguiluz sees no benefit for now this PR and #620 may be closed.

@javiereguiluz
Copy link
Member

Let's ask other regular contributors of this repository to see if they agree with this change or prefer to close it (and add it back later when things improve): @yceruto @voronkovich @bocharsky-bw. Thanks!

@yceruto
Copy link
Member

yceruto commented Oct 4, 2017

Expected argument of type "DateTimeImmutable", "DateTime" given

Could not convert PHP value of type 'DateTime' to type 'datetime_immutable'. Expected one of the following types: null, DateTimeImmutable

Sadly the Form Component is not ready completely to return a DateTimeImmutable instance upon reverse transformation:

https://github.com/symfony/symfony/blob/0082981e4135cf6c04545156db8fcda2c9459f9a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php#L72

So we might have to do something to fix it, probably into DateTimePickerType and then I'm not sure if these changes are worth it.

{
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.

@voronkovich
Copy link
Contributor

I agree with @jkufner that the usage of the DataTimeImmutable makes an application more robust. So, I think we should wait until the Form component has been fixed.

@@ -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.

@javiereguiluz
Copy link
Member

If the Form component doesn't support immutable dates, that's a huge blocker for me. Is there any open pull request to improve the Form component about this? Will it be merged soon? If not, I'm afraid we should close this as "can't fix" until the Form component improves.

@gabel
Copy link
Author

gabel commented Oct 6, 2017

As already mentioned in #620 - I guess that somebody has to pick up this unfinished PR symfony/symfony#19889 before it makes sense to continue here.

@voronkovich
Copy link
Contributor

@javiereguiluz, @gabel we could use a datetime_immutable type only for the Comment::publishedAt property.

@javiereguiluz
Copy link
Member

Let's close this until Symfony Forms fully support immutable dates (which sadly could take a lot of time!) Meanwhile, I've extracted in #669 the part of this pull request unrelated to immutable dates. Thank you all for the discussion.

javiereguiluz added a commit that referenced this pull request Oct 9, 2017
This PR was merged into the master branch.

Discussion
----------

Use the new "json" type from DBAL 2.6

This partially fixes #620 and it's based on #659.

Commits
-------

189083f Use the new "json" type from DBAL 2.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants