@@ -39,8 +39,8 @@ Creating a Document Class
39
39
Without thinking about Doctrine or PHPCR-ODM, you can create a ``Task `` object
40
40
in PHP::
41
41
42
- // src/Acme/TaskBundle /Document/Task.php
43
- namespace Acme\TaskBundle \Document;
42
+ // src/App /Document/Task.php
43
+ namespace use App \Document;
44
44
45
45
class Task
46
46
{
@@ -57,8 +57,8 @@ Doctrine PHPCR-ODM yet - it's just a simple PHP class.
57
57
.. note ::
58
58
59
59
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 .
62
62
63
63
Add Mapping Information
64
64
~~~~~~~~~~~~~~~~~~~~~~~
@@ -78,8 +78,8 @@ class via annotations:
78
78
79
79
.. code-block :: php-annotations
80
80
81
- // src/Acme/TaskBundle /Document/Task.php
82
- namespace Acme\TaskBundle \Document;
81
+ // src/App /Document/Task.php
82
+ namespace App \Document;
83
83
84
84
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
85
85
@@ -111,8 +111,8 @@ class via annotations:
111
111
112
112
.. code-block :: yaml
113
113
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 :
116
116
id : id
117
117
118
118
fields :
@@ -123,7 +123,7 @@ class via annotations:
123
123
124
124
.. code-block :: xml
125
125
126
- <!-- src/Acme/TaskBundle /Resources/config/doctrine/Task.phpcr.xml -->
126
+ <!-- src/App /Resources/config/doctrine/Task.phpcr.xml -->
127
127
<?xml version =" 1.0" encoding =" UTF-8" ?>
128
128
<doctrine-mapping
129
129
xmlns =" http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping"
@@ -132,7 +132,7 @@ class via annotations:
132
132
https://github.com/doctrine/phpcr-odm/raw/master/doctrine-phpcr-odm-mapping.xsd"
133
133
>
134
134
135
- <document name =" Acme\TaskBundle \Document\Task" >
135
+ <document name =" App \Document\Task" >
136
136
137
137
<id name =" id" />
138
138
@@ -181,21 +181,19 @@ Persisting Documents to PHPCR
181
181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
182
182
183
183
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::
187
186
188
- // src/Acme/TaskBundle /Controller/DefaultController.php
187
+ // src/App /Controller/DefaultController.php
189
188
190
189
// ...
191
- use Acme\TaskBundle\Document\Task;
190
+ use App\Document\Task;
191
+ use Doctrine\ODM\PHPCR\DocumentManagerInterface;
192
192
use Symfony\Component\HttpFoundation\Response;
193
193
194
194
// ...
195
- public function createAction()
195
+ public function createAction(DocumentManagerInterface $documentManager )
196
196
{
197
- $documentManager = $this->get('doctrine_phpcr')->getManager();
198
-
199
197
$rootTask = $documentManager->find(null, '/tasks');
200
198
201
199
$task = new Task();
@@ -211,22 +209,21 @@ AcmeTaskBundle::
211
209
212
210
Take a look at the previous example in more detail:
213
211
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
219
217
configure a :ref: `Repository Initializer <phpcr-odm-repository-initializers >`,
220
218
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 ``
222
220
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.
230
227
231
228
When creating or updating objects, the workflow is always the same. In the
232
229
next section, you'll see how Doctrine is smart enough to update documents if
@@ -238,9 +235,12 @@ Fetching Objects from PHPCR
238
235
Fetching an object back out of PHPCR is even easier. For example, suppose
239
236
you've configured a route to display a specific task by name::
240
237
241
- public function showAction($name)
238
+ use App\Document\Task;
239
+ use Doctrine\ODM\PHPCR\DocumentManagerInterface;
240
+
241
+ public function showAction(DocumentManagerInterface $documentManager, $name)
242
242
{
243
- $repository = $this->get('doctrine_phpcr')-> getRepository('AcmeTaskBundle: Task' );
243
+ $repository = $documentManager-> getRepository(Task::class );
244
244
$task = $repository->find('/tasks/'.$name);
245
245
246
246
if (!$task) {
@@ -263,12 +263,12 @@ The repository contains all sorts of helpful methods::
263
263
$task = $repository->find($id);
264
264
265
265
// 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] );
267
267
268
268
// query for all tasks matching the name, ordered by done
269
269
$tasks = $repository->findBy(
270
- array( 'name' => 'foo') ,
271
- array( 'done' => 'ASC')
270
+ [ 'name' => 'foo'] ,
271
+ [ 'done' => 'ASC']
272
272
);
273
273
274
274
.. tip ::
@@ -291,10 +291,12 @@ Updating an Object
291
291
Once you've fetched an object from Doctrine, updating it is easy. Suppose you
292
292
have a route that maps a task ID to an update action in a controller::
293
293
294
- public function updateAction($name)
294
+ use App\Document\Task;
295
+ use Doctrine\ODM\PHPCR\DocumentManagerInterface;
296
+
297
+ public function updateAction(DocumentManagerInterface $documentManager, $name)
295
298
{
296
- $documentManager = $this->get('doctrine_phpcr')->getManager();
297
- $repository = $documentManager->getRepository('AcmeTaskBundle:Task');
299
+ $repository = $documentManager->getRepository(Task::class);
298
300
$task = $repository->find('/tasks/'.$name);
299
301
300
302
if (!$task) {
0 commit comments