8000 Merge pull request #848 from symfony-cmf/autowire-phpcr-odm · torreytsui/symfony-cmf-docs@99c81eb · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 99c81eb

Browse files
authored
Merge pull request symfony-cmf#848 from symfony-cmf/autowire-phpcr-odm
adjust phpcr-odm documentation to autowiring
2 parents 4b0abce + 2e4b6f2 commit 99c81eb

File tree

8 files changed

+98
-83
lines changed

8 files changed

+98
-83
lines changed

bundles/phpcr_odm/configuration.rst

-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ The service to use as base for building the PHPCR-ODM configuration.
538538
**type**: ``boolean``, **default**: ``true``
539539

540540
When enabled, you can place your mappings in
541-
``<Bundle>/Resources/config/doctrine/<Document>.phpcr.xml`` resp. ``*.phpcr.yml``
542-
to configure mappings for documents you provide in the ``<Bundle>/Document``
541+
``<App|Bundle>/Resources/config/doctrine/<Document>.phpcr.xml`` resp. ``*.phpcr.yml``
542+
to configure mappings for documents you provide in the ``<App|Bundle>/Document``
543543
folder. Otherwise you need to manually configure the mappings section.
544544

545545
``auto_generate_proxy_classes``

bundles/phpcr_odm/events.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use this configuration:
3333
# app/config/services.yml
3434
services:
3535
app.phpcr_search_indexer:
36-
class: AppBundle\EventListener\SearchIndexer
36+
class: App\EventListener\SearchIndexer
3737
tags:
3838
- { name: doctrine_phpcr.event_listener, event: postPersist }
3939
4040
app.phpcr_listener:
41-
class: AppBundle\EventListener\MyListener
41+
class: App\EventListener\MyListener
4242
tags:
4343
- { name: doctrine_phpcr.event_subscriber }
4444
@@ -49,11 +49,11 @@ use this configuration:
4949
<container xmlns="http://symfony.com/schema/dic/services">
5050
<services>
5151
<service id="app.phpcr_search_indexer"
52-
class="AppBundle\EventListener\SearchIndexer">
52+
class="App\EventListener\SearchIndexer">
5353
<tag name="doctrine_phpcr.event_listener" event="postPersist" />
5454
</service>
5555
<service id="app.phpcr_listener"
56-
class="AppBundle\EventListener\MyListener">
56+
class="App\EventListener\MyListener">
5757
<tag name="doctrine_phpcr.event_subscriber" />
5858
</service>
5959
</services>
@@ -62,8 +62,8 @@ use this configuration:
6262
.. code-block:: php
6363
6464
// app/config/config.php
65-
use AppBundle\EventListener\SearchIndexer;
66-
use AppBundle\EventListener\MyListener;
65+
use App\EventListener\SearchIndexer;
66+
use App\EventListener\MyListener;
6767
6868
$container
6969
->register(

bundles/phpcr_odm/fixtures_initializers.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Repository Initializers
1616
-----------------------
1717

1818
The Initializer is the PHPCR equivalent of the ORM schema tools. It is used to
19-
let bundles register PHPCR node types and to create required base paths in the
19+
let your application PHPCR node types and to create required base paths in the
2020
repository.
2121

2222
.. note::
@@ -98,10 +98,10 @@ specific documents, you need your own Initializer. The interesting method
9898
to overwrite is the ``init`` method. It is passed the ``ManagerRegistry``,
9999
from which you can retrieve the PHPCR session but also the document manager::
100100

101-
// src/AppBundle/Initializer/SiteInitializer.php
102-
namespace AppBundle\Initializer;
101+
// src/App/Initializer/SiteInitializer.php
102+
namespace App\Initializer;
103103

104-
use AppBundle\Documents\Site;
104+
use App\Documents\Site;
105105
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface;
106106
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
107107
use PHPCR\SessionInterface;
@@ -153,7 +153,7 @@ Define a service for your Initializer as follows:
153153
services:
154154
# ...
155155
app.phpcr_initializer_site:
156-
class: AppBundle\Initializer\SiteInitializer
156+
class: App\Initializer\SiteInitializer
157157
tags:
158158
- { name: doctrine_phpcr.initializer }
159159
@@ -171,7 +171,7 @@ Define a service for your Initializer as follows:
171171
<services>
172172
<!-- ... -->
173173
<service id="app.phpcr_initializer_site"
174-
class="AppBundle\Initializer\SiteInitializer">
174+
class="App\Initializer\SiteInitializer">
175175
<tag name="doctrine_phpcr.initializer"/>
176176
</service>
177177
</services>
@@ -186,7 +186,7 @@ Define a service for your Initializer as follows:
186186
$container
187187
->register(
188188
'app.phpcr_initializer_site',
189-
'AppBundle\Initializer\SiteInitializer'
189+
'App\Initializer\SiteInitializer'
190190
)
191191
->addTag('doctrine_phpcr.initializer', ['name' => 'doctrine_phpcr.initializer']
192192
;
@@ -208,7 +208,7 @@ basis.
208208
209209
# app/config/services.yml
210210
app.migration:
211-
class: AppBundle\Migration\Migration
211+
class: App\Migration\Migration
212212
arguments:
213213
- { "%app.content_basepath%", "%app.menu_basepath%" }
214214
tags:
@@ -220,7 +220,7 @@ basis.
220220
<?xml version="1.0" ?>
221221
<container xmlns="http://symfony.com/schema/dic/services">
222222
<service id="app.migration"
223-
class="AppBundle\Migration\Migration">
223+
class="App\Migration\Migration">
224224
<argument type="collection">
225225
<argument>%app.content_basepath%</argument>
226226
<argument>%app.menu_basepath%</argument>
@@ -232,7 +232,7 @@ basis.
232232
233233
.. code-block:: php
234234
235-
use AppBundle\Migration\Migration;
235+
use App\Migration\Migration;
236236
use Symfony\Component\DependencyInjection\Definition;
237237
238238
// ...
@@ -275,13 +275,13 @@ install the `DoctrineFixturesBundle`_ which brings the
275275

276276
Fixtures work the same way they work for Doctrine ORM. You write fixture
277277
classes implementing ``Doctrine\Common\DataFixtures\FixtureInterface``. If you
278-
place them in ``<Bundle>\DataFixtures\PHPCR``, they will be auto detected if you
278+
place them in ``<App|Bundle>\DataFixtures\PHPCR``, they will be auto detected if you
279279
don't specify a path in the command.
280280

281281
A simple example fixture class looks like this::
282282

283-
// src/AppBundle/DataFixtures/PHPCR/LoadPageData.php
284-
namespace AppBundle\DataFixtures\PHPCR;
283+
// src/App/DataFixtures/PHPCR/LoadPageData.php
284+
namespace App\DataFixtures\PHPCR;
285285

286286
use Doctrine\Common\Persistence\ObjectManager;
287287
use Doctrine\Common\DataFixtures\FixtureInterface;

bundles/phpcr_odm/forms.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ that the option for the document manager is called ``em``.
3838

3939
A simple example of using the ``phpcr_document`` form type looks as follows::
4040

41-
use AppBundle\Document\TargetClass;
41+
use App\Document\TargetClass;
4242

4343
$form
4444
->add(
@@ -82,7 +82,7 @@ targets as an array of PHPCR-ODM ids or PHPCR paths.
8282

8383
The minimal code required to use this type looks as follows::
8484

85-
use AppBundle\Document\Article;
85+
use App\Document\Article;
8686

8787
$dataArr = [
8888
'/some/phpcr/path/item_1' => 'first item',
@@ -126,14 +126,14 @@ correct.
126126

127127
.. code-block:: yaml
128128
129-
# src/AppBundle/Resources/config/validation.yml
130-
AppBundle\Entity\Author:
129+
# src/App/Resources/config/validation.yml
130+
App\Document\Author:
131131
constraints:
132132
- Doctrine\Bundle\PHPCRBundle\Validator\Constraints\ValidPhpcrOdm
133133
134134
.. code-block:: php-annotations
135135
136-
// src/AppBundle/Entity/Author.php
136+
// src/App/Document/Author.php
137137
138138
// ...
139139
use Doctrine\Bundle\PHPCRBundle\Validator\Constraints as OdmAssert;
@@ -155,15 +155,15 @@ correct.
155155
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
156156
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
157157
158-
<class name="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route">
158+
<class name="App\Document\Author">
159159
<constraint name="Doctrine\Bundle\PHPCRBundle\Validator\Constraints\ValidPhpcrOdm" />
160160
</class>
161161
162162
</constraint-mapping>
163163
164164
.. code-block:: php
165165
166-
// src/AppBundle/Entity/Author.php
166+
// src/App/Document/Author.php
167167
168168
// ...
169169
use Symfony\Component\Validator\Mapping\ClassMetadata;

bundles/phpcr_odm/introduction.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ here, the ODM services will not be loaded.
320320
Unless you disable ``auto_mapping``, you can place your documents in the
321321
``Document`` folder inside your bundles and use annotations or name the
322322
mapping files following this schema:
323-
``<Bundle>/Resources/config/doctrine/<DocumentClass>.phpcr.xml`` or ``*.phpcr.yml``.
323+
``<App|Bundle>/Resources/config/doctrine/<DocumentClass>.phpcr.xml`` or ``*.phpcr.yml``.
324324

325325
If ``auto_generate_proxy_classes`` is false, you need to run the
326326
``cache:warmup`` command in order to have the proxy classes generated after
@@ -413,11 +413,13 @@ Services
413413

414414
There are 3 main services provided by this bundle:
415415

416-
* ``doctrine_phpcr``- The ``ManagerRegistry`` instance with references to all
417-
sessions and document manager instances;
418-
* ``doctrine_phpcr.default_session`` - The PHPCR session instance;
419-
* ``doctrine_phpcr.odm.default_document_manager`` - The PHPCR-ODM document
420-
manager instance.
416+
* ``Doctrine\Bundle\PHPCRBundle\ManagerRegistry``- The ``ManagerRegistry``
417+
instance with references to all sessions and document manager instances;
418+
* ``PHPCR\SessionInterface`` - the PHPCR session. If you configured
419+
multiple sessions, this will be the default session;
420+
* ``Doctrine\ODM\PHPCR\DocumentManagerInterface`` - the PHPCR-ODM document
421+
manager. If you configured multiple managers, this will be the default
422+
manager.
421423

422424
.. _bundles-phpcr-odm-commands:
423425

bundles/phpcr_odm/models.rst

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Creating a Document Class
3939
Without thinking about Doctrine or PHPCR-ODM, you can create a ``Task`` object
4040
in PHP::
4141

42-
// src/Acme/TaskBundle/Document/Task.php
43-
namespace Acme\TaskBundle\Document;
42+
// src/App/Document/Task.php
43+
namespace use App\Document;
4444

4545
class Task
4646
{
@@ -57,8 +57,8 @@ Doctrine PHPCR-ODM yet - it's just a simple PHP class.
5757
.. note::
5858

5959
A Document is analogous to the term ``Entity`` employed by the Doctrine
60-
ORM. You must add this object to the ``Document`` sub-namespace of you
61-
bundle, in order register the mapping data automatically.
60+
ORM. To have the mapping happen automatically, place your documents in the
61+
``Document`` namespace within your application.
6262

6363
Add Mapping Information
6464
~~~~~~~~~~~~~~~~~~~~~~~
@@ -78,8 +78,8 @@ class via annotations:
7878

7979
.. code-block:: php-annotations
8080
81-
// src/Acme/TaskBundle/Document/Task.php
82-
namespace Acme\TaskBundle\Document;
81+
// src/App/Document/Task.php
82+
namespace App\Document;
8383
8484
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
8585
@@ -111,8 +111,8 @@ class via annotations:
111111
112112
.. code-block:: yaml
113113
114-
# src/Acme/TaskBundle/Resources/config/doctrine/Task.phpcr.yml
115-
Acme\TaskBundle\Document\Task:
114+
# src/App/Resources/config/doctrine/Task.phpcr.yml
115+
App\Document\Task:
116116
id: id
117117
118118
fields:
@@ -123,7 +123,7 @@ class via annotations:
123123
124124
.. code-block:: xml
125125
126-
<!-- src/Acme/TaskBundle/Resources/config/doctrine/Task.phpcr.xml -->
126+
<!-- src/App/Resources/config/doctrine/Task.phpcr.xml -->
127127
<?xml version="1.0" encoding="UTF-8" ?>
128128
<doctrine-mapping
129129
xmlns="http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping"
@@ -132,7 +132,7 @@ class via annotations:
132132
https://github.com/doctrine/phpcr-odm/raw/master/doctrine-phpcr-odm-mapping.xsd"
133133
>
134134
135-
<document name="Acme\TaskBundle\Document\Task">
135+
<document name="App\Document\Task">
136136
137137
<id name="id" />
138138
@@ -181,21 +181,19 @@ Persisting Documents to PHPCR
181181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
182182

183183
Now that you have a mapped ``Task`` document, complete with getter and setter
184-
methods, you're ready to persist data to PHPCR. From inside a controller,
185-
this is pretty easy, add the following method to the ``DefaultController`` of the
186-
AcmeTaskBundle::
184+
methods, you are ready to persist data to PHPCR. For a simple example, lets do
185+
this from inside a controller::
187186

188-
// src/Acme/TaskBundle/Controller/DefaultController.php
187+
// src/App/Controller/DefaultController.php
189188

190189
// ...
191-
use Acme\TaskBundle\Document\Task;
190+
use App\Document\Task;
191+
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
192192
use Symfony\Component\HttpFoundation\Response;
193193

194194
// ...
195-
public function createAction()
195+
public function createAction(DocumentManagerInterface $documentManager)
196196
{
197-
$documentManager = $this->get('doctrine_phpcr')->getManager();
198-
199197
$rootTask = $documentManager->find(null, '/tasks');
200198

201199
$task = new Task();
@@ -211,22 +209,21 @@ AcmeTaskBundle::
211209

212210
Take a look at the previous example in more detail:
213211

214-
* **line 10** This line fetches Doctrine's *document manager* object, which is
215-
responsible for handling the process of persisting and fetching objects to
216-
and from PHPCR.
217-
* **line 12** This line fetches the root document for the tasks, as each
218-
Document needs to have a parent. To create this root document, you can
212+
* **line 8** We use symfony controller injection with autowiring to get the
213+
*document manager*. This service is responsible for storing and fetching
214+
objects to and from PHPCR.
215+
* **line 10** This line loads the root document for the tasks, as each PHPCR
216+
document needs to have a parent. To create this root document, you can
219217
configure a :ref:`Repository Initializer <phpcr-odm-repository-initializers>`,
220218
which will be executed when running ``doctrine:phpcr:repository:init``.
221-
* **lines 14-16** In this section, you instantiate and work with the ``$task``
219+
* **lines 12-14** In this section, you instantiate and work with the ``$task``
222220
object like any other, normal PHP object.
223-
* **line 18** The ``persist()`` method tells Doctrine to "manage" the
224-
``$task`` object. This does not actually cause a query to be made to PHPCR
225-
(yet).
226-
* **line 20** When the ``flush()`` method is called, Doctrine looks through
227-
all of the objects that it is managing to see if they need to be persisted to
228-
PHPCR. In this example, the ``$task`` object has not been persisted yet, so
229-
the document manager makes a query to PHPCR, which adds a new document.
221+
* **line 16** The ``persist()`` method tells Doctrine to "manage" the ``$task``
222+
object. This does not actually cause a query to be made to PHPCR (yet).
223+
* **line 20** When the ``flush()`` method is called, Doctrine looks through all
224+
of the objects that it is managing to see if they need to be stored to PHPCR.
225+
In this example, the ``$task`` object has not been saved yet, so the document
226+
manager makes a query to PHPCR to add it.
230227

231228
When creating or updating objects, the workflow is always the same. In the
232229
next section, you'll see how Doctrine is smart enough to update documents if
@@ -238,9 +235,12 @@ Fetching Objects from PHPCR
238235
Fetching an object back out of PHPCR is even easier. For example, suppose
239236
you've configured a route to display a specific task by name::
240237

241-
public function showAction($name)
238+
use App\Document\Task;
239+
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
240+
241+
public function showAction(DocumentManagerInterface $documentManager, $name)
242242
{
243-
$repository = $this->get('doctrine_phpcr')->getRepository('AcmeTaskBundle:Task');
243+
$repository = $documentManager->getRepository(Task::class);
244244
$task = $repository->find('/tasks/'.$name);
245245

246246
if (!$task) {
@@ -263,12 +263,12 @@ The repository contains all sorts of helpful methods::
263263
$task = $repository->find($id);
264264

265265
// query for one task matching be name and done
266-
$task = $repository->findOneBy(array('name' => 'foo', 'done' => false));
266+
$task = $repository->findOneBy(['name' => 'foo', 'done' => false]);
267267

268268
// query for all tasks matching the name, ordered by done
269269
$tasks = $repository->findBy(
270-
array('name' => 'foo'),
271-
array('done' => 'ASC')
270+
['name' => 'foo'],
271+
['done' => 'ASC']
272272
);
273273

274274
.. tip::
@@ -291,10 +291,12 @@ Updating an Object
291291
Once you've fetched an object from Doctrine, updating it is easy. Suppose you
292292
have a route that maps a task ID to an update action in a controller::
293293

294-
public function updateAction($name)
294+
use App\Document\Task;
295+
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
296+
297+
public function updateAction(DocumentManagerInterface $documentManager, $name)
295298
{
296-
$documentManager = $this->get('doctrine_phpcr')->getManager();
297-
$repository = $documentManager->getRepository('AcmeTaskBundle:Task');
299+
$repository = $documentManager->getRepository(Task::class);
298300
$task = $repository->find('/tasks/'.$name);
299301

300302
if (!$task) {

0 commit comments

Comments
 (0)
0