Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
weaverryan wants to merge3 commits intosymfony:masterfromweaverryan:doctrine-types-update
Closed
Changes fromall 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
49 changes: 21 additions & 28 deletionsdoctrine.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
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 :)

{
// 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);

Expand All@@ -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());
}

// youcan also receivethe$em as an argument
public function editAction(EntityManagerInterface $em)
//ifyouhave multiple entity managers, usetheregistry to fetch them
public function editAction(ManagerRegistry $doctrine)
{
// ...
$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 usegetManagerForClass() instead)?

}

.. 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``
Expand DownExpand Up@@ -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')
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 theProduct::class usage?

Copy link
MemberAuthor

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
MemberAuthor

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 :)

yceruto reacted with thumbs up emoji
->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
MemberAuthor

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 :)


if (!$product) {
Expand All@@ -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::

Expand All@@ -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);
Expand All@@ -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(
Expand DownExpand Up@@ -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) {
Expand DownExpand Up@@ -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');
Expand All@@ -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
Expand DownExpand Up@@ -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"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp