-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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() | ||
public function createAction(EntityManagerInterface $em) | ||
{ | ||
// 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!'); | ||
|
||
$em = $this->getDoctrine()->getManager(); | ||
|
||
// 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()); | ||
} | ||
|
||
// you can also receive the $em as an argument | ||
public function editAction(EntityManagerInterface $em) | ||
// if you have multiple entity managers, use the registry to fetch them | ||
public function editAction(ManagerRegistry $doctrine) | ||
{ | ||
// ... | ||
$em = $doctrine->getManager(); | ||
$em2 = $doctrine->getManager('other_connection') | ||
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. | ||
|
||
.. tip:: | ||
|
||
This article shows working with Doctrine from within a controller by using | ||
the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getDoctrine` | ||
method of the controller. This method is a shortcut to get the | ||
``doctrine`` service. You can work with Doctrine anywhere else | ||
by injecting that service in the service. See | ||
:doc:`/service_container` for more on creating your own services. | ||
|
||
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:: | ||
|
||
public function showAction($productId) | ||
use Doctrine\ORM\EnityManagerInterface; | ||
|
||
public function showAction($productId, EnityManagerInterface $em) | ||
{ | ||
$product = $this->getDoctrine() | ||
->getRepository('AppBundle:Product') | ||
$product = $em->getRepository('AppBundle:Product') | ||
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 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 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); | ||
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] 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 = $this->getDoctrine() | ||
->getRepository('AppBundle:Product'); | ||
$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 = $this->getDoctrine()->getRepository('AppBundle:Product'); | ||
$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 = $this->getDoctrine()->getRepository('AppBundle:Product'); | ||
$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:: | ||
|
||
public function updateAction($productId) | ||
use Doctrine\ORM\EnityManagerInterface; | ||
|
||
public function updateAction($productId, EntityManagerInterface $em) | ||
{ | ||
$em = $this->getDoctrine()->getManager(); | ||
$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 = $this->getDoctrine()->getRepository('AppBundle:Product'); | ||
$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:: | ||
|
||
$em = $this->getDoctrine()->getManager(); | ||
$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 = $this->getDoctrine() | ||
->getRepository('AppBundle:Product'); | ||
$repository = $em->getRepository('AppBundle:Product'); | ||
|
||
// createQueryBuilder() automatically selects FROM AppBundle:Product | ||
// and aliases it to "p" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing use statement :)