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