Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -549,17 +549,19 @@ a controller, this is pretty easy. Add the following method to the | ||
| use AppBundle\Entity\Product; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Doctrine\Common\Persistence\ManagerRegistry; | ||
| // ... | ||
| public function createAction(EntityManagerInterface $em) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. missing use statement :) | ||
| { | ||
| // or fetch the em via the container | ||
| // $em = $this->get('doctrine')->getManager(); | ||
| $product = new Product(); | ||
| $product->setName('Keyboard'); | ||
| $product->setPrice(19.99); | ||
| $product->setDescription('Ergonomic and stylish!'); | ||
| // tells Doctrine you want to (eventually) save the Product (no queries yet) | ||
| $em->persist($product); | ||
| @@ -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()); | ||
| } | ||
| //ifyouhave multiple entity managers, usetheregistry to fetch them | ||
| public function editAction(ManagerRegistry $doctrine) | ||
| { | ||
| $em = $doctrine->getManager(); | ||
| $em2 = $doctrine->getManager('other_connection') | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| .. note:: | ||
| If you're following along with this example, you'll need to create a | ||
| route that points to this action to see it work. | ||
| Take a look at the previous example in more detail: | ||
| * **lines 10-13** In this section, you instantiate and work with the ``$product`` | ||
| @@ -638,10 +632,11 @@ Fetching an object back out of the database is even easier. For example, | ||
| suppose you've configured a route to display a specific ``Product`` based | ||
| on its ``id`` value:: | ||
| use Doctrine\ORM\EnityManagerInterface; | ||
| public function showAction($productId, EnityManagerInterface $em) | ||
| { | ||
| $product = $em->getRepository('AppBundle:Product') | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [DX] What about to promote the MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Opened#7892 to keep track of this :) | ||
| ->find($productId); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [DX] MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) | ||
| if (!$product) { | ||
| @@ -664,8 +659,7 @@ as its "repository". You can think of a repository as a PHP class whose only | ||
| job is to help you fetch entities of a certain class. You can access the | ||
| repository object for an entity class via:: | ||
| $repository = $em->getRepository('AppBundle:Product'); | ||
| .. note:: | ||
| @@ -676,7 +670,7 @@ repository object for an entity class via:: | ||
| Once you have a repository object, you can access all sorts of helpful methods:: | ||
| $repository = $em->getRepository('AppBundle:Product'); | ||
| // query for a single product by its primary key (usually "id") | ||
| $product = $repository->find($productId); | ||
| @@ -699,7 +693,7 @@ Once you have a repository object, you can access all sorts of helpful methods:: | ||
| You can also take advantage of the useful ``findBy()`` and ``findOneBy()`` methods | ||
| to easily fetch objects based on multiple conditions:: | ||
| $repository = $em->getRepository('AppBundle:Product'); | ||
| // query for a single product matching the given name and price | ||
| $product = $repository->findOneBy( | ||
| @@ -732,9 +726,10 @@ Updating an Object | ||
| Once you've fetched an object from Doctrine, updating it is easy. Suppose | ||
| you have a route that maps a product id to an update action in a controller:: | ||
| use Doctrine\ORM\EnityManagerInterface; | ||
| public function updateAction($productId, EntityManagerInterface $em) | ||
| { | ||
| $product = $em->getRepository('AppBundle:Product')->find($productId); | ||
| if (!$product) { | ||
| @@ -781,7 +776,7 @@ Querying for Objects | ||
| You've already seen how the repository object allows you to run basic queries | ||
| without any work:: | ||
| $repository = $em->getRepository('AppBundle:Product'); | ||
| $product = $repository->find($productId); | ||
| $product = $repository->findOneByName('Keyboard'); | ||
| @@ -801,7 +796,6 @@ Imagine that you want to query for products that cost more than ``19.99``, | ||
| ordered from least to most expensive. You can use DQL, Doctrine's native | ||
| SQL-like language, to construct a query for this scenario:: | ||
| $query = $em->createQuery( | ||
| 'SELECT p | ||
| FROM AppBundle:Product p | ||
| @@ -841,8 +835,7 @@ Instead of writing a DQL string, you can use a helpful object called the | ||
| depends on dynamic conditions, as your code soon becomes hard to read with | ||
| DQL as you start to concatenate strings:: | ||
| $repository = $em->getRepository('AppBundle:Product'); | ||
| // createQueryBuilder() automatically selects FROM AppBundle:Product | ||
| // and aliases it to "p" | ||