@@ -16,22 +16,24 @@ setup a document like the following with a ``$keywords`` property that is mapped
16
16
17
17
<?php
18
18
19
- namespace Documents;
20
-
21
19
#[Document]
22
20
class Product
23
21
{
24
22
#[Id]
25
- private $id;
23
+ public string $id;
26
24
27
25
#[Field(type: 'string')]
28
- private $title;
26
+ public string $title;
29
27
28
+ /** @var Collection<string > */
30
29
#[Field(type: 'collection')]
31
30
#[Index]
32
- private $keywords = [] ;
31
+ public Collection $keywords;
33
32
34
- // ...
33
+ public function __construct()
34
+ {
35
+ $this->keywords = new ArrayCollection();
36
+ }
35
37
}
36
38
37
39
Working with Keywords
@@ -44,12 +46,12 @@ Now, create a product and add some keywords:
44
46
<?php
45
47
46
48
$product = new Product();
47
- $product->setTitle( 'Nike Air Jordan 2011') ;
48
- $product->addKeyword ('nike shoes');
49
- $product->addKeyword ('jordan shoes');
50
- $product->addKeyword ('air jordan');
51
- $product->addKeyword ('shoes');
52
- $product->addKeyword ('2011');
49
+ $product->title = 'Nike Air Jordan 2011';
50
+ $product->keywords->add ('nike shoes');
51
+ $product->keywords->add ('jordan shoes');
52
+ $product->keywords->add ('air jordan');
53
+ $product->keywords->add ('shoes');
54
+ $product->keywords->add ('2011');
53
55
54
56
$dm->persist($product);
55
57
$dm->flush();
@@ -106,8 +108,9 @@ the results to your query. Here is an example:
106
108
Embedded Documents
107
109
------------------
108
110
109
- If you want to use an embedded document instead of just an array then you can. It will allow you to store
110
- additional information with each keyword, like its weight.
111
+ If you want to use an embedded document instead of just an array then you can.
112
+ That will allow you to store additional information with each keyword, like its
113
+ weight.
111
114
112
115
Definition
113
116
~~~~~~~~~~
@@ -121,20 +124,14 @@ You can setup a ``Keyword`` document like the following:
121
124
#[EmbeddedDocument]
122
125
class Keyword
123
126
{
124
- #[Field(type: 'string')]
125
- #[Index]
126
- private $keyword;
127
-
128
- #[Field(type: 'int')]
129
- private $weight;
130
-
131
- public function __construct(string $keyword, int $weight)
132
- {
133
- $this->keyword = $keyword;
134
- $this->weight = $weight;
135
- }
136
-
137
- // ...
127
+ public function __construct(
128
+ #[Field(type: 'string')]
129
+ #[Index]
130
+ public $keyword,
131
+
132
+ #[Field(type: 'int')]
133
+ public $weight,
134
+ ) {}
138
135
}
139
136
140
137
Now you can embed the ``Keyword `` document many times in the ``Product ``:
@@ -150,8 +147,9 @@ Now you can embed the ``Keyword`` document many times in the ``Product``:
150
147
{
151
148
// ...
152
149
150
+ /** @var Collection<Keyword > */
153
151
#[EmbedMany(targetDocument: Keyword::class)]
154
- private $keywords;
152
+ public Collection $keywords;
155
153
156
154
// ...
157
155
}
@@ -163,10 +161,12 @@ you would have to do the following:
163
161
164
162
<?php
165
163
166
- $product->addKeyword (new Keyword('nike shoes', 1));
164
+ $product->keywords->add (new Keyword('nike shoes', 1));
167
165
168
- This is a very basic search engine example and can work for many small and simple applications. If you
169
- need better searching functionality you can look at integrating something like `Solr `_ in your project.
166
+ This is a very basic search engine example and can work for many small and
167
+ simple applications. If you need better searching functionality you can look at
168
+ `MongoDB Atlas Search `_, which can be integrated using
169
+ :doc: `Search Indexes <../reference/search-indexes >`
170
170
171
171
.. _AlchemyAPI : http://www.alchemyapi.com
172
- .. _ Solr : http ://lucene.apache.org/solr
172
+ .. _ MongoDB Atlas Search : https ://www.mongodb.com/products/platform/atlas-search
0 commit comments