@@ -39,8 +39,8 @@ Creating a Document Class
3939Without thinking about Doctrine or PHPCR-ODM, you can create a ``Task `` object
4040in 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
6363Add 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
183183Now 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
212210Take 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
BFC1
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
231228When creating or updating objects, the workflow is always the same. In the
232229next section, you'll see how Doctrine is smart enough to update documents if
@@ -238,9 +235,12 @@ Fetching Objects from PHPCR
238235Fetching an object back out of PHPCR is even easier. For example, suppose
239236you'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
291291Once you've fetched an object from Doctrine, updating it is easy. Suppose you
292292have 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