Skip to content

Example of the Doctrine doc with types as the first-class citizen #7876

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
wants to merge 3 commits into from
Closed

Example of the Doctrine doc with types as the first-class citizen #7876

wants to merge 3 commits into from

Conversation

weaverryan
Copy link
Member

This updates the doctrine chapter in the new "types-first" philosophy - i.e. using type-hinting and autowiring to fetch services (rather than service ids). Of course, we must still show both (the type hint and the service id), but probably only the first time in a chapter. Afterwards, we should show the type.

This is the first "normal" chapter to get this update. Thoughts?

Thanks!

@weaverryan weaverryan added this to the 3.3 milestone May 5, 2017
doctrine.rst Outdated
// you can also receive the $em as an argument
public function editAction(EntityManagerInterface $em)
// you can use the registry to fetch other em's, if you have mutiple
public function editAction(ManagerRegistry $registry)
Copy link
Member

Choose a reason for hiding this comment

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

A question newcomers (and not only them) may ask: why sometimes I need to inject an interface (EntityManagerInterface) and sometimes just a class (ManagerRegistry).

Copy link
Member Author

Choose a reason for hiding this comment

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

Bah, that's Doctrine's fault. ManagerRegistry IS an interface... I think they've only more recently started to use the Interface suffix.

Btw, I'm hoping the new debug:container --types option will help this out - since that valid type-hints

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

Great! 👍 Just minor (optional) dx comments :)

{
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
$product = $em->getRepository('AppBundle:Product')
Copy link
Member

Choose a reason for hiding this comment

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

[DX] What about to promote the Product::class usage?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we should start doing this :). But it's gotta be in a different PR, because it needs to be applied to 3.1 and higher

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #7892 to keep track of this :)

{
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
$product = $em->getRepository('AppBundle:Product')
->find($productId);
Copy link
Member

Choose a reason for hiding this comment

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

[DX] $em->find(Product::class, $productId)? at the end it does the same, so less coding :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, it's cool ... but then you haven't taught the user about the repository. So, I like the slightly longer, but "simpler" approach - tell them that you need the repo 100% of the time :)

doctrine.rst Outdated
// you can also receive the $em as an argument
public function editAction(EntityManagerInterface $em)
// you can use the registry to fetch other em's, if you have mutiple
public function editAction(ManagerRegistry $registry)
Copy link
Member

Choose a reason for hiding this comment

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

$doctrine?

Copy link
Member Author

Choose a reason for hiding this comment

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

+1

Copy link
Contributor

@GuilhemN GuilhemN left a comment

Choose a reason for hiding this comment

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

awesome 👍


// ...
public function createAction()
public function createAction(EntityManagerInterface $em)
Copy link
Contributor

Choose a reason for hiding this comment

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

missing use statement :)

@GuilhemN
Copy link
Contributor

GuilhemN commented May 6, 2017

I'm wondering, should we assert everywhere that folders registered in the standard edition do not need to be registered again in the docs?

doctrine.rst Outdated
@@ -569,26 +571,18 @@ a controller, this is pretty easy. Add the following method to the
return new Response('Saved new product with id '.$product->getId());
}

// you can also receive the $em as an argument
public function editAction(EntityManagerInterface $em)
// you can use the registry to fetch other em's, if you have mutiple
Copy link
Member

Choose a reason for hiding this comment

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

you can use the registry to fetch other em's, if you have mutiple

-->

if you have multiple entity managers, use the registry to fetch them

@weaverryan
Copy link
Member Author

I'm wondering, should we assert everywhere that folders registered in the standard edition do not need to be registered again in the docs?

@GuilhemN Let's have that discussion on a different PR - like #7883, or another that I'll hopefully open today (this PR just doesn't have any good examples of that as far as I can see). But I'm totally wondering about that. Unless... we can get symfony/symfony-standard#1070 merged... then it won't matter (and this is one of the reasons I want that feature!)

@weaverryan weaverryan deleted the doctrine-types-update branch May 10, 2017 09:51
{
// ...
$em = $doctrine->getManager();
$em2 = $doctrine->getManager('other_connection')
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if it is common to get the manager for a particular connection. Don't you rather want to get the entity manager that manages a particular entity (i.e. you will want to use getManagerForClass() instead)?

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.

5 participants