@@ -23,7 +23,7 @@ data stored with PHPCR has a relationship with at least one other data: its
23
23
parent. The inverse relation also exists, you can also get the children of a
24
24
data element.
25
25
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
27
27
downloaded in the previous chapter. Go to your directory and execute the
28
28
following command:
29
29
@@ -36,30 +36,43 @@ The result will be the PHPCR tree:
36
36
.. code-block :: text
37
37
38
38
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).
58
67
59
68
Each 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 ``
61
70
node. 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
63
76
64
77
.. note ::
65
78
@@ -74,20 +87,21 @@ Doctrine PHPCR-ODM
74
87
The Symfony CMF uses the `Doctrine PHPCR-ODM `_ to interact with PHPCR.
75
88
Doctrine allows a user to create objects (called *documents *) which are
76
89
directly 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.
78
92
79
93
Creating a Page with code
80
94
-------------------------
81
95
82
96
Now 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.
86
100
87
101
First, you have to create a new DataFixture to add your new page. You do this
88
102
by creating a new class in the AppBundle::
89
103
90
- // src/AppBundle/DataFixtures/PHPCR/LoadPageData .php
104
+ // src/AppBundle/DataFixtures/PHPCR/LoadQuickTourData .php
91
105
namespace AppBundle\DataFixtures\PHPCR;
92
106
93
107
use Doctrine\Common\Persistence\ObjectManager;
@@ -100,7 +114,7 @@ by creating a new class in the AppBundle::
100
114
{
101
115
// refers to the order in which the class' load function is called
102
116
// (lower return values are called first)
103
- return 10 ;
117
+ return 100 ;
104
118
}
105
119
106
120
public function load(ObjectManager $documentManager)
@@ -123,7 +137,10 @@ PHPCR. But first, you have to create a new Page document::
123
137
}
124
138
125
139
$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
+
127
144
$page->setLabel('Another new Page');
128
145
$page->setTitle('Another new Page');
129
146
$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
133
150
node. To do this, we first retrieve the root document from PHPCR and then set
134
151
it as its parent::
135
152
136
- // ...
137
153
public function load(ObjectManager $documentManager)
138
154
{
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
-
144
155
// ...
145
-
146
156
// get root document (/cms/simple)
147
157
$simpleCmsRoot = $documentManager->find(null, '/cms/simple');
148
158
@@ -152,21 +162,46 @@ it as its parent::
152
162
And at last, we have to tell the Document Manager to persist our Page
153
163
document using the Doctrine API::
154
164
155
- // ...
156
165
public function load(ObjectManager $documentManager)
157
166
{
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
-
163
167
// ...
164
168
$documentManager->persist($page); // add the Page in the queue
165
169
$documentManager->flush(); // add the Page to PHPCR
166
170
}
167
171
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!
170
205
171
206
.. image :: ../_images/quick_tour/the-model-new-page.png
172
207
0 commit comments