@@ -23,7 +23,7 @@ data stored with PHPCR has a relationship with at least one other data: its
2323parent. The inverse relation also exists, you can also get the children of a
2424data element.
2525
26- Let's take a look at the dump of the tree of the Standard Edition you
26+ Let's take a look at the dump of the tree of the CMF Sandbox you
2727downloaded in the previous chapter. Go to your directory and execute the
2828following command:
2929
@@ -36,30 +36,43 @@ The result will be the PHPCR tree:
3636.. code-block :: text
3737
3838 ROOT:
39- cms:
40- simple:
41- about:
42- contact:
43- map:
44- team:
45- quick_tour:
46- dynamic:
47- docs:
48- demo:
49- demo_redirect:
50- hardcoded_dynamic:
51- hardcoded_static:
52-
53- Each data is called a *node * in PHPCR. In this tree, there are 13 nodes and
54- one ROOT node (created by PHPCR). You may have already seen the document you
55- created in the previous section, it's called ``quick_tour `` (and it's path is
56- ``/cms/simple/quick_tour ``). When using the SimpleCmsBundle, all nodes are
57- stored in the ``/cms/simple `` path.
39+ cms:
40+ menu:
41+ main:
42+ admin-item:
43+ projects-item:
44+ cmf-item:
45+ company-item:
46+ team-item:
47+ ...
48+ content:
49+ home:
50+ phpcr_locale:en:
51+ phpcr_locale:fr:
52+ phpcr_locale:de:
53+ seoMetadata:
54+ additionalInfoBlock:
55+ child1:
56+ ...
57+ routes:
58+ en:
59+ company:
60+ team:
61+ more:
62+ about:
63+ ...
64+
65+ Each data is called a *node * in PHPCR. Everything is attached under the ROOT
66+ node (created by PHPCR itself).
5867
5968Each node has properties, which contain the data. The content, title and label
60- you set for your page are saved in such properties for the ``quick_tour ``
69+ you set for your page are saved in such properties for the ``home ``
6170node. You can view these properties by adding the ``--props `` switch to the
62- dump command.
71+ dump command:
72+
73+ .. code-block :: bash
74+
75+ $ php bin/console doctrine:phpcr:node:dump --props /cms/content/home
6376
6477 .. note ::
6578
@@ -74,20 +87,21 @@ Doctrine PHPCR-ODM
7487The Symfony CMF uses the `Doctrine PHPCR-ODM `_ to interact with PHPCR.
7588Doctrine allows a user to create objects (called *documents *) which are
7689directly persisted into and retrieved from the PHPCR tree. This is similar to
77- the Doctrine ORM used by the Symfony2 Framework, but then for PHPCR.
90+ the Doctrine ORM provided by default in the Symfony Standard Edition, but for
91+ PHPCR instead of SQL databases.
7892
7993Creating a Page with code
8094-------------------------
8195
8296Now you know a little bit more about PHPCR and you know the tool to interact
83- with it, you can start using it yourself. In the previous chapter, you created
84- a page by using a yaml file which was parsed by the SimpleCmsBundle. This
85- time, you'll create a page by doing it yourself.
97+ with it, you can start using it yourself. In the previous chapter, you edited
98+ a page by using a yaml file which was parsed by the fixture loader of the
99+ sandbox. This time, you'll create a page by doing it yourself.
86100
87101First, you have to create a new DataFixture to add your new page. You do this
88102by creating a new class in the AppBundle::
89103
90- // src/AppBundle/DataFixtures/PHPCR/LoadPageData .php
104+ // src/AppBundle/DataFixtures/PHPCR/LoadQuickTourData .php
91105 namespace AppBundle\DataFixtures\PHPCR;
92106
93107 use Doctrine\Common\Persistence\ObjectManager;
@@ -100,7 +114,7 @@ by creating a new class in the AppBundle::
100114 {
101115 // refers to the order in which the class' load function is called
102116 // (lower return values are called first)
103- return 10 ;
117+ return 100 ;
104118 }
105119
106120 public function load(ObjectManager $documentManager)
@@ -123,7 +137,10 @@ PHPCR. But first, you have to create a new Page document::
123137 }
124138
125139 $page = new Page(); // create a new Page object (document)
126- $page->setName('new_page'); // the name of the node
140+ $page->setName('quick-tour'); // the name of the node
141+
142+ vno sandbox übernehmen
143+
127144 $page->setLabel('Another new Page');
128145 $page->setTitle('Another new Page');
129146 $page->setBody('I have added this page myself!');
@@ -133,16 +150,9 @@ Each document needs a parent. In this case, the parent should just be the root
133150node. To do this, we first retrieve the root document from PHPCR and then set
134151it as its parent::
135152
136- // ...
137153 public function load(ObjectManager $documentManager)
138154 {
139- if (!$documentManager instanceof DocumentManager) {
140- $class = get_class($documentManager);
141- throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
142- }
143-
144155 // ...
145-
146156 // get root document (/cms/simple)
147157 $simpleCmsRoot = $documentManager->find(null, '/cms/simple');
148158
@@ -152,21 +162,46 @@ it as its parent::
152162And at last, we have to tell the Document Manager to persist our Page
153163document using the Doctrine API::
154164
155- // ...
156165 public function load(ObjectManager $documentManager)
157166 {
158- if (!$documentManager instanceof DocumentManager) {
159- $class = get_class($documentManager);
160- throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '$class' given.");
161- }
162-
163167 // ...
164168 $documentManager->persist($page); // add the Page in the queue
165169 $documentManager->flush(); // add the Page to PHPCR
166170 }
167171
168- Now you need to execute the ``doctrine:phpcr:fixtures:load `` command again and
169- then you can visit your website again. You'll see your new page you added!
172+ Now you need to execute the ``doctrine:phpcr:fixtures:load `` command again.
173+ When dumping the nodes again, your new page should turn up under /cms/content.
174+
175+ To actually see this page in the browser, we need a route::
176+
177+ public function load(ObjectManager $documentManager)
178+ {
179+ // ...
180+ $route = new Route();
181+ $routeRoot = $documentManager->find(null, '/cms/routes/en');
182+ $route->setPosition($routeRoot, 'quick-tour');
183+ $route->setContent($page);
184+ $documentManager->persist($route);
185+ }
186+
187+ And we add a menu entry to link to this page, and flush the document manager
188+ at the end::
189+
190+ public function load(ObjectManager $documentManager)
191+ {
192+ $menu = new MenuNode();
193+ $menu->setName('new_page');
194+ $menu->setLabel('Quick Tour');
195+ $menu->setContent($page);
196+ $menuMain = $documentManager->find(null, '/cms/menu/main');
197+ $menu->setParentDocument($menuMain);
198+ $documentManager->persist($menu);
199+
200+ $documentManager->flush();
201+ }
202+
203+ Re-run the fixtures loading command and then refresh the web site. Your new
204+ page will be added, with a menu entry at the bottom of the menu!
170205
171206.. image :: ../_images/quick_tour/the-model-new-page.png
172207
0 commit comments