MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
persistentcriteria.class
Ir para a documentação deste ficheiro.
1<?php
2
4{
5 private $name;
6 private $classMap;
7 private $associationMap;
8 private $joinType;
9 private $alias;
10
11 public function __construct($name, $classMap, $aMap, $alias, $joinType)
12 {
13 $this->name = $name;
14 $this->classMap = $classMap;
15 $this->associationMap = $aMap;
16 $this->alias = $alias;
17 $this->joinType = $joinType;
18 }
19
20 public function getAssociationMap()
21 {
22 return $this->associationMap;
23 }
24
25 public function getName()
26 {
27 return $this->name;
28 }
29
30 public function getAlias()
31 {
32 return $this->alias;
33 }
34
35 public function setAlias($alias)
36 {
37 $this->alias = $alias;
38 }
39
40 public function getJoinType()
41 {
42 return $this->joinType;
43 }
44
45 public function setJoinType($joinType)
46 {
47 $this->joinType = $joinType;
48 }
49
50 public function getClassMap()
51 {
52 return $this->classMap;
53 }
54}
55
57{
58 protected $classMap = NULL;
59 protected $tables = array();
60 protected $aliasTable = array();
61 protected $associations = array();
62 protected $whereCondition = NULL;
63 protected $manager = NULL;
64 protected $alias = '';
65
66 public function __construct($classMap, $manager)
67 {
68 $this->classMap = $classMap;
69 $this->manager = $manager;
70 // Fill tables with tableMaps
71 $cm = $this->classMap;
72
73 do
74 {
75 $this->tables['table'][] = $cm->getTable();
76 $this->tables['alias'][] = '';
77 $cm = $cm->getSuperClass();
78 } while ($cm != NULL);
79
80 // Create condition for the WHERE part of this criteria
81 $this->whereCondition = $this->getNewCondition();
82 }
83
84 public function getManager()
85 {
86 return $this->manager;
87 }
88
89 public function getNewCondition()
90 {
91 return new CriteriaCondition();
92 }
93
94 public function getWhereCondition()
95 {
97 }
98
99 public function getClassMap()
100 {
101 return $this->classMap;
102 }
103
104 public function addTable($tableMap, $alias = '')
105 {
106 if (($i = array_search($tableMap, $this->tables['table'])) === FALSE)
107 {
108 $this->tables['table'][] = $tableMap;
109 $this->tables['alias'][] = $alias;
110 }
111 else
112 {
113 if ($alias != '')
114 {
115 $a = $this->tables['alias'][$i];
116
117 if ($a != '')
118 {
119 $this->tables['table'][] = $tableMap;
120 $this->tables['alias'][] = $alias;
121 }
122 else
123 {
124 $this->tables['alias'][$i] = $alias;
125 }
126 }
127 }
128 }
129
130 public function getTableAlias($tableMap)
131 {
132 if (($i = array_search($tableMap, $this->tables['table'])) === FALSE)
133 {
134 return '';
135 }
136 else
137 {
138 return $this->tables['alias'][$i];
139 }
140 }
141
142 public function addCriteriaTables($tables)
143 {
144 $n = count($tables['table']);
145
146 for ($i = 0; $i < $n; $i++)
147 {
148 $this->addTable($tables['table'][$i], $tables['alias'][$i]);
149 }
150 }
151
152 public function getTables()
153 {
154 return $this->tables;
155 }
156
157 public function getAlias($classMap = NULL)
158 {
159 if ($className == NULL)
160 return $this->alias;
161 else
162 {
163 $alias = array_search($classMap, $this->aliasTable);
164 return ($alias) ? $alias : '';
165 }
166 }
167
168 public function setAlias($alias, $classMap = NULL)
169 {
170 if ($classMap == NULL)
171 {
172 $this->alias = $alias;
174 }
175
176 $this->aliasTable[$alias] = $classMap;
177 $this->addTable($classMap->getTable(), $alias);
178 }
179
180 public function isAlias($name)
181 {
182 return isset($this->aliasTable[$name]);
183 }
184
185 public function setAssociationAlias($associationName, $alias)
186 {
187 $association = $this->getAssociation($associationName);
188
189 if ($association == NULL)
190 {
191 $this->addAssociationMap($this->classMap, $associationName);
192 $association = $this->associations[$associationName];
193 }
194
195 $association->setAlias($alias);
196 $cm = $association->getAssociationMap()->getForClass();
197 $this->setAlias($alias, $cm);
198 }
199
200 public function setAssociationType($associationName, $joinType)
201 {
202 $association = $this->getAssociation($associationName);
203
204 if ($association == NULL)
205 {
206 $this->addAssociationMap($this->classMap, $associationName);
207 $association = $this->associations[$associationName];
208 }
209
210 $association->setJoinType($joinType);
211 }
212
213 public function setAutoAssociationAlias($alias0, $alias1)
214 {
215 $this->setAlias($alias0, $this->classMap);
216 $this->setAlias($alias1, $this->classMap);
217 }
218
219 public function setReferenceAlias($alias)
220 {
221 $this->aliasTable[$alias] = $this->classMap;
222 }
223
224 public function getAssociationsJoin()
225 {
226 // Build a join array to sql statement
227 $join = array();
228
229 // Inheritance associations
231
232 do
233 {
234 for ($i = 0; $i < $classMap->getReferenceSize(); $i++)
235 {
236 $am = $classMap->getReferenceAttributeMap($i);
237
238 if ($cm = $am->getColumnMap())
239 {
240 $crm = $am->getReference()->getColumnMap();
241 $t1 = $cm->getTableMap();
242 $t1Alias = $this->getTableAlias($t1);
243 $t1Name = $t1->getName() . ' ' . $t1Alias;
244 $t2 = $crm->getTableMap();
245 $t2Alias = $this->getTableAlias($t2);
246 $t2Name = $t2->getName() . ' ' . $t2Alias;
247 $condition = $cm->getFullyQualifiedName($t1Alias) . "=" . $crm->getFullyQualifiedName($t2Alias);
248 $join[] = array($t1Name, $t2Name, $condition, 'INNER');
249 }
250 }
251
252 $classMap = $classMap->getSuperClass();
253 } while ($classMap != NULL);
254
255 // if this classMap has joinAssociations, add to $this->associations
256 if ($this->classMap->getJoinAssociationSize())
257 {
258 foreach($this->classMap->getJoinAssociationMaps() as $aMap)
259 {
260 $this->_addAssociation($aMap->getTargetName(),$this->classMap, $aMap, '', $aMap->getJoinAutomatic());
261 }
262 }
263
264 // Associations
265 $n = count($this->associations);
266
267 if ($n > 0)
268 {
269 foreach ($this->associations as $association)
270 {
271 $aMap = $association->getAssociationMap();
272 $type = strtoupper($association->getJoinType());
273 $k = $aMap->getSize();
274
275 for ($j = 0; $j < $k; $j++)
276 {
277 $entry = $aMap->getEntry($j);
278 $t1 = $entry->getFrom()->getColumnMap()->getTableMap();
279 $t1Alias = ($aMap->IsInverse() ? $association->getAlias() : $this->getTableAlias($t1));
280 $t1Name = $t1->getName() . ' ' . $t1Alias;
281 $t2 = $entry->getTo()->getColumnMap()->getTableMap();
282 $t2Alias = (!$aMap->IsInverse() ? $association->getAlias() : $this->getTableAlias($t2));
283 $t2Name = $t2->getName() . ' ' . $t2Alias;
284
285 if ($aMap->IsInverse())
286 {
287 $condition = $entry->getTo()->getColumnMap()->getFullyQualifiedName($t2Alias) . "=" . $entry->getFrom()->getColumnMap()->getFullyQualifiedName($t1Alias);
288 $join[] = array($t2Name, $t1Name, $condition, $type);
289 }
290 else
291 {
292 $condition = $entry->getFrom()->getColumnMap()->getFullyQualifiedName($t1Alias) . "=" . $entry->getTo()->getColumnMap()->getFullyQualifiedName($t2Alias);
293 $join[] = array($t1Name, $t2Name, $condition, $type);
294 }
295 }
296 }
297 }
298
299 return (count($join) ? $join : NULL);
300 }
301
302 public function getAttributeMap(&$attribute)
303 {
304 $map = NULL;
305 $cm = $this->classMap;
306
307 if (strpos($attribute, '.'))
308 {
309 $tok = strtok($attribute, ".");
310
311 while ($tok)
312 {
313 $nameSequence[] = $tok;
314 $tok = strtok(".");
315 }
316
317 for ($i = 0; $i < count($nameSequence) - 1; $i++)
318 {
319 $name = $nameSequence[$i];
320 $isAlias = $this->isAlias($name);
321
322 if ($isAlias)
323 {
324 $cm = $this->aliasTable[$name];
325 break;
326 }
327 else
328 {
329 $am = $this->getAssociationMap($cm, $name);
330
331 // If association map is NULL something wrong with names
332 if ($am == NULL)
333 break;
334
335 $cm = $am->getForClass();
336 }
337 }
338
339 if ($cm != NULL)
340 {
341 $attribute = $nameSequence[count($nameSequence) - 1];
342 $map = $cm->getAttributeMap($attribute, TRUE);
343
344 if ($map == NULL)
345 {
346 if ($isAlias)
347 {
348 $attribute = $name . '.' . $attribute;
349 }
350 else
351 {
352 $attribute = implode('.', $nameSequence);
353 }
354 }
355 }
356 }
357 else
358 {
359 $map = $cm->getAttributeMap($attribute, TRUE);
360
361 if (($map != NULL) && ($this->alias != ''))
362 $attribute = $this->alias . '.' . $attribute;
363 }
364
365 return $map;
366 }
367
368 public function getOperand($operand)
369 {
370 if ($operand == NULL)
371 {
372 $o = new OperandNull($this, $operand);
373 }
374 elseif (is_object($operand))
375 {
376 if ($operand instanceof AttributeMap)
377 {
378 $o = new OperandAttributeMap($this, $operand, $operand->getName());
379 }
380 elseif ($operand instanceof RetrieveCriteria)
381 {
382 $o = new OperandCriteria($this, $operand);
383 }
384 else
385 {
386 $o = new OperandObject($this, $operand);
387 }
388 }
389 elseif (is_array($operand))
390 {
391 $o = new OperandArray($this, $operand);
392 }
393 elseif (strpos($operand, '(') === FALSE)
394 {
395 $op = $operand;
396 $am = $this->getAttributeMap($operand);
397
398 if ($am == NULL)
399 {
400 $o = new OperandValue($this, $operand);
401 }
402 else
403 {
404 $o = new OperandAttributeMap($this, $am, $operand);
405 }
406 }
407 else
408 {
409 $o = new OperandFunction($this, $operand);
410 }
411
412 return $o;
413 }
414
415 public function getCriteria($op1, $operator = '', $op2 = NULL)
416 {
417 $operand1 = $this->getOperand($op1);
418 $operand2 = $this->getOperand($op2);
419 $criteria = new BaseCriteria($operand1, $operator, $operand2);
420 return $criteria;
421 }
422
423 public function addCriteria($op1, $operator = '', $op2 = NULL)
424 {
425 $this->whereCondition->addCriteria($this->getCriteria($op1, $operator, $op2));
426 }
427
428 public function addOrCriteria($op1, $operator = '', $op2 = NULL)
429 {
430 $this->whereCondition->addOrCriteria($this->getCriteria($op1, $operator, $op2));
431 }
432
433 public function getAssociation($associationName)
434 {
435 $association = NULL;
436
437 foreach ($this->associations as $a)
438 {
439 if (($a->getName() == $associationName) || ($a->getAlias() == $associationName))
440 $association = $a;
441 }
442
443 return $association;
444 }
445
446 public function getAssociationMap($classMap, $associationName)
447 {
448 $association = $this->getAssociation($associationName);
449
450 if ($association == NULL)
451 {
452 if ($this->addAssociationMap($classMap, $associationName) != NULL)
453 $association = $this->associations[$associationName];
454 else
455 return NULL;
456 }
457
458 return $association->getAssociationMap();
459 }
460
461 public function addAssociationMap($classMap, $associationName, $inverse = NULL)
462 {
463 $am = $classMap->getAssociationMap($associationName);
464 if ($inverse !== NULL)
465 {
466 $am->SetInverse($inverse);
467 }
468
469 if ($am != NULL)
470 {
471 $cardinality = $am->getCardinality();
472
473 if ($cardinality == 'manyToMany')
474 {
475 $direction = $am->getDirection();
476 $this->addAssociationMap($am->getAssociativeClass(), $direction[0], TRUE);
477 $this->addAssociationMap($am->getAssociativeClass(), $direction[1], FALSE);
478 }
479 else
480 {
481 $this->_addAssociation($associationName, $classMap, $am);
482 }
483
484 $this->addTable($classMap->getTable());
485
486 if (($cm = $am->getForClass()) != NULL)
487 {
488 $this->addTable($cm->getTable());
489 }
490 }
491
492 return $am;
493 }
494
495 private function _addAssociation($name, $classMap, $associationMap, $alias = '', $joinType = 'INNER')
496 {
497 $this->associations[$name] = new AssociationCriteria($name, $classMap, $associationMap, $alias, $joinType);
498 }
499
500 public function addJoinCriteria($criteria)
501 {
502 $this->addCriteriaTables($criteria->getTables());
503 $this->associations = array_merge($this->associations, $criteria->associations);
504 $this->whereCondition->addCondition($criteria->getWhereCondition());
505 }
506}
507?>
__construct($name, $classMap, $aMap, $alias, $joinType)
setAutoAssociationAlias($alias0, $alias1)
__construct($classMap, $manager)
getAssociationMap($classMap, $associationName)
getCriteria($op1, $operator='', $op2=NULL)
getAssociation($associationName)
addTable($tableMap, $alias='')
setAssociationType($associationName, $joinType)
addOrCriteria($op1, $operator='', $op2=NULL)
setAlias($alias, $classMap=NULL)
addAssociationMap($classMap, $associationName, $inverse=NULL)
addCriteria($op1, $operator='', $op2=NULL)
setAssociationAlias($associationName, $alias)