| author | ymh <ymh.work@gmail.com> |
| Wed, 09 Nov 2011 16:25:13 +0100 | |
| changeset 30 | d2fba1e3b94b |
| parent 29 | 7496254cfead |
| child 34 | 21fab44f46fe |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
2 |
||
3 |
namespace IRI\Bundle\WikiTagBundle\Entity; |
|
4 |
||
5 |
use Doctrine\ORM\EntityRepository; |
|
| 18 | 6 |
use IRI\Bundle\WikiTagBundle\Entity\Document; |
| 29 | 7 |
use Doctrine\ORM\Query\ResultSetMapping; |
| 18 | 8 |
use \ReflectionClass; |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
9 |
use Doctrine\ORM\AbstractQuery; |
| 2 | 10 |
|
11 |
/** |
|
12 |
* DocumentRepository |
|
13 |
* |
|
14 |
* This class was generated by the Doctrine ORM. Add your own custom |
|
15 |
* repository methods below. |
|
16 |
*/ |
|
17 |
class DocumentRepository extends EntityRepository |
|
18 |
{ |
|
| 18 | 19 |
/** |
20 |
* |
|
21 |
* TODO : Enter description here ... |
|
22 |
* @var ReflectionClass |
|
23 |
*/ |
|
24 |
private $reflection_class; |
|
25 |
private $reflection_doc_class; |
|
26 |
private $set_methods = array(); |
|
27 |
private $get_methods = array(); |
|
28 |
||
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
29 |
function findOneByExternalId($external_id) |
|
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
30 |
{ |
| 18 | 31 |
return $this->findOneBy(array("externalId" => $external_id)); |
32 |
} |
|
33 |
||
34 |
private function reflectionSetField($object, $method_name, $value) |
|
35 |
{ |
|
36 |
if(isset($this->set_methods[$method_name])) |
|
37 |
{ |
|
38 |
$set_method = $this->set_methods[$method_name]; |
|
39 |
} |
|
40 |
||
41 |
if(!isset($set_method) || is_null($set_method)) |
|
42 |
{ |
|
43 |
if(is_null($this->reflection_doc_class)) |
|
44 |
{ |
|
45 |
$this->reflection_doc_class = new ReflectionClass(get_class($object)); |
|
46 |
} |
|
47 |
||
48 |
$set_method = NULL; |
|
49 |
if($this->reflection_doc_class->hasMethod($method_name)) |
|
50 |
{ |
|
51 |
$set_method = $this->reflection_doc_class->getMethod($method_name); |
|
52 |
} |
|
53 |
if(!is_null($set_method)) |
|
54 |
{ |
|
55 |
$this->set_methods[$method_name]=$set_method; |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
if(!isset($set_method) || is_null($set_method) || !$set_method->isPublic()) |
|
60 |
{ |
|
61 |
throw new \Exception("setter method unknown $method_name"); |
|
62 |
} |
|
63 |
||
64 |
//set value |
|
65 |
$set_method->invoke($object, $value); |
|
66 |
||
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
67 |
} |
| 3 | 68 |
|
| 18 | 69 |
private function reflectionGetField($document, $accessor) |
70 |
{ |
|
71 |
||
72 |
if(!isset($this->get_methods[$accessor]) || is_null($this->get_methods[$accessor])) |
|
73 |
{ |
|
74 |
if(is_null($this->reflection_class)) |
|
75 |
{ |
|
76 |
$this->reflection_class = new \ReflectionClass(get_class($document)); |
|
77 |
} |
|
78 |
||
79 |
//look at properties |
|
80 |
if($this->reflection_class->hasProperty($accessor)) |
|
81 |
{ |
|
82 |
$get_object = $this->reflection_class->getProperty($accessor); |
|
83 |
if(!$get_object->isPublic()) |
|
84 |
{ |
|
85 |
$get_object = NULL; |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod($accessor)) |
|
90 |
{ |
|
91 |
$get_object = $this->reflection_class->getMethod($accessor); |
|
92 |
if(!$get_object->isPublic()) |
|
93 |
{ |
|
94 |
$get_object = NULL; |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod("get".ucfirst($accessor))) |
|
99 |
{ |
|
100 |
$get_object = $this->reflection_class->getMethod("get".ucfirst($accessor)); |
|
101 |
if(!$get_object->isPublic()) |
|
102 |
{ |
|
103 |
$get_object = NULL; |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
if(isset($get_object) && !is_null($get_object)) |
|
108 |
{ |
|
109 |
$this->get_methods[$accessor] = $get_object; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
if(isset($this->get_methods[$accessor])) |
|
114 |
{ |
|
115 |
$get_object = $this->get_methods[$accessor]; |
|
116 |
if(!is_null($get_object)) |
|
117 |
{ |
|
118 |
if(is_a($get_object,"\ReflectionMethod")) |
|
119 |
{ |
|
120 |
return $get_object->invoke($document); |
|
121 |
} |
|
122 |
elseif(is_a($get_object,"\ReflectionProperty")) |
|
123 |
{ |
|
124 |
return $get_object->getValue($document); |
|
125 |
} |
|
126 |
else |
|
127 |
{ |
|
128 |
//TODO : custom exception |
|
129 |
throw new \Exception("Bad reflection object type"); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
//TODO: replace by custom exception |
|
135 |
throw new \Exception("Unknown accessor $accessor"); |
|
136 |
} |
|
137 |
||
138 |
function writeDocument($document, $document_id_column, $fields) |
|
| 3 | 139 |
{ |
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
140 |
// get document from id |
|
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
141 |
|
| 18 | 142 |
$docid = $this->reflectionGetField($document, $document_id_column); |
143 |
$baseDocument = $this->findOneByExternalId($docid); |
|
| 3 | 144 |
|
145 |
if(is_null($baseDocument)) |
|
146 |
{ |
|
147 |
$baseDocument = new Document(); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
148 |
$baseDocument->setExternalId($document); |
| 3 | 149 |
} |
| 18 | 150 |
|
151 |
foreach ($fields as $name => $field_def) { |
|
152 |
if(isset($field_def['accessor'])) |
|
153 |
{ |
|
154 |
$accessor = $field_def['accessor']; |
|
155 |
} |
|
156 |
else |
|
157 |
{ |
|
158 |
$accessor = NULL; |
|
159 |
} |
|
160 |
if(is_null($accessor)) |
|
161 |
{ |
|
162 |
$accessor = $name; |
|
163 |
} |
|
164 |
||
165 |
$value = strval($this->reflectionGetField($document,$accessor)); |
|
166 |
||
167 |
$method_name = "set".ucfirst($name); |
|
168 |
||
169 |
$this->reflectionSetField($baseDocument, $method_name, $value); |
|
170 |
||
171 |
} |
|
| 3 | 172 |
|
173 |
$this->getEntityManager()->persist($baseDocument); |
|
174 |
$this->getEntityManager()->flush(); |
|
175 |
return $baseDocument; |
|
176 |
||
177 |
} |
|
178 |
||
| 18 | 179 |
function removeDocument($document, $document_id_column) |
| 3 | 180 |
{ |
| 18 | 181 |
$docid = $this->reflectionGetField($document, $document_id_column); |
182 |
$baseDocument = $this->findOneByExternalId($docid); |
|
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
183 |
if(!is_null($baseDocument)) |
|
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
184 |
{ |
| 3 | 185 |
$this->getEntityManager()->remove($baseDocument); |
186 |
} |
|
187 |
} |
|
188 |
||
| 27 | 189 |
function getTagsStr($document) |
190 |
{ |
|
191 |
$em = $this->getEntityManager(); |
|
192 |
$query = $em->createQuery("SELECT t.label FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :docid"); |
|
193 |
$query = $query->setParameter("docid", $document); |
|
194 |
$result = $query->getScalarResult(); |
|
195 |
$tagstr = array(); |
|
196 |
foreach ($result as $res) { |
|
197 |
$tagstr[] = $res['label']; |
|
198 |
} |
|
199 |
return $tagstr; |
|
200 |
} |
|
201 |
||
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
202 |
function updateTagsStr($document) |
|
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
203 |
{ |
| 27 | 204 |
|
205 |
$tagstr = $this->getTagsStr($document); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
206 |
|
| 27 | 207 |
$document->setTagsStr(implode(",",$tagstr)); |
208 |
$this->getEntityManager()->persist($document); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
209 |
} |
| 29 | 210 |
|
211 |
/** |
|
212 |
* |
|
213 |
* Enter description here ... |
|
214 |
* @param array $values : key: the fields to search into, value array('value'=>value, 'weight'=>weight) |
|
215 |
* @param unknown_type $conditions |
|
216 |
*/ |
|
217 |
function search(array $values, array $conditions=NULL) |
|
218 |
{ |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
219 |
$em = $this->getEntityManager(); |
| 29 | 220 |
|
221 |
$rsm = new ResultSetMapping(); |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
222 |
$rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d"); |
| 29 | 223 |
$rsm->addFieldResult("d", "id", "id"); |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
224 |
$rsm->addScalarResult("score", "score"); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
225 |
$rsm->addMetaResult("d", "external_id", "externalId"); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
226 |
|
| 29 | 227 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
228 |
$score = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
229 |
$i = 0; |
| 29 | 230 |
foreach ($values as $fielddef) { |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
231 |
$i++; |
| 29 | 232 |
$columns = $fielddef["columns"]; |
233 |
$value = $fielddef["value"]; |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
234 |
$weight = isset($fielddef["weight"])?$fielddef["weight"]:1.0; |
| 29 | 235 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
236 |
$score[] = "(MATCH($columns) AGAINST (:value_$i))*:weight_$i"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
237 |
$parameters["value_$i"] = $value; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
238 |
$parameters["weight_$i"] = $weight; |
| 29 | 239 |
} |
240 |
||
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
241 |
$score_def = "(".implode("+", $score).")"; |
| 29 | 242 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
243 |
$conditions_str = ""; |
| 29 | 244 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
245 |
if(!is_null($conditions)) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
246 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
247 |
$conditions_array = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
248 |
$i = 0; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
249 |
foreach ($conditions as $field => $conddef) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
250 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
251 |
$i++; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
252 |
if(is_array($conddef) && isset($conddef['operator'])) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
253 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
254 |
$operator = $conddef["operator"]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
255 |
$values = $conddef["value"]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
256 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
257 |
elseif(is_array($conddef)) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
258 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
259 |
$operator = "IN"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
260 |
$values = $conddef; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
261 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
262 |
else |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
263 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
264 |
$operator = "="; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
265 |
$values = $conddef; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
266 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
267 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
268 |
if($operator === "IN") |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
269 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
270 |
$in_parameters = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
271 |
for ($j = 0; $j < count($values); $j++) { |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
272 |
$parameters["cond_val_$i_$j"] = $values[$j]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
273 |
$in_parameters[] = ":cond_val_$i_$j"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
274 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
275 |
$cond = "($field IN (".implode(",",$in_parameters)."))"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
276 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
277 |
else |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
278 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
279 |
$cond = "($field $operator :cond_val_$i)"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
280 |
$parameters["cond_val_$i"] = $values; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
281 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
282 |
$conditions_array[] = $cond; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
283 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
284 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
285 |
if(count($conditions_array) > 0) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
286 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
287 |
$conditions_str = " AND ".implode(" AND ", $conditions_array); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
288 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
289 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
290 |
} |
| 29 | 291 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
292 |
$query = $em->createNativeQuery("SELECT d.id, d.external_id, $score_def AS score FROM wikitag_document d WHERE $score_def > 0 $conditions_str ORDER BY score DESC", $rsm); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
293 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
294 |
$query->setParameters($parameters); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
295 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
296 |
$res = $query->getResult(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
297 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
298 |
return $res; |
| 29 | 299 |
|
300 |
||
301 |
} |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
302 |
|
| 2 | 303 |
} |