| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfDoctrineRoute extends sfObjectRoute |
|---|
| 24 |
{ |
|---|
| 25 |
protected |
|---|
| 26 |
$query = null; |
|---|
| 27 |
|
|---|
| 28 |
public function setListQuery(Doctrine_Query $query) |
|---|
| 29 |
{ |
|---|
| 30 |
if (!$this->isBound()) |
|---|
| 31 |
{ |
|---|
| 32 |
throw new LogicException('The route is not bound.'); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
$this->query = $query; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
protected function getObjectForParameters($parameters) |
|---|
| 39 |
{ |
|---|
| 40 |
$results = $this->getObjectsForParameters($parameters); |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
// need to return the first Doctrine_Record |
|---|
| 44 |
if ($results instanceof Doctrine_Collection) |
|---|
| 45 |
{ |
|---|
| 46 |
if (count($results)) |
|---|
| 47 |
{ |
|---|
| 48 |
$results = $results->getFirst(); |
|---|
| 49 |
} else { |
|---|
| 50 |
$results = null; |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
else if(!is_object($results)) |
|---|
| 55 |
{ |
|---|
| 56 |
$results = null; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
return $results; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
protected function getObjectsForParameters($parameters) |
|---|
| 63 |
{ |
|---|
| 64 |
$tableModel = Doctrine_Core::getTable($this->options['model']); |
|---|
| 65 |
|
|---|
| 66 |
$variables = array(); |
|---|
| 67 |
$values = array(); |
|---|
| 68 |
foreach($this->getRealVariables() as $variable) |
|---|
| 69 |
{ |
|---|
| 70 |
if($tableModel->hasColumn($tableModel->getColumnName($variable))) |
|---|
| 71 |
{ |
|---|
| 72 |
$variables[] = $variable; |
|---|
| 73 |
$values[$variable] = $parameters[$variable]; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
if (!isset($this->options['method'])) |
|---|
| 78 |
{ |
|---|
| 79 |
if (null === $this->query) |
|---|
| 80 |
{ |
|---|
| 81 |
$q = $tableModel->createQuery('a'); |
|---|
| 82 |
foreach ($values as $variable => $value) |
|---|
| 83 |
{ |
|---|
| 84 |
$fieldName = $tableModel->getFieldName($variable); |
|---|
| 85 |
$q->andWhere('a.'. $fieldName . ' = ?', $parameters[$variable]); |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
else |
|---|
| 89 |
{ |
|---|
| 90 |
$q = $this->query; |
|---|
| 91 |
} |
|---|
| 92 |
if (isset($this->options['method_for_query'])) |
|---|
| 93 |
{ |
|---|
| 94 |
$method = $this->options['method_for_query']; |
|---|
| 95 |
$results = $tableModel->$method($q); |
|---|
| 96 |
} |
|---|
| 97 |
else |
|---|
| 98 |
{ |
|---|
| 99 |
$results = $q->execute(); |
|---|
| 100 |
} |
|---|
| 101 |
} |
|---|
| 102 |
else |
|---|
| 103 |
{ |
|---|
| 104 |
$method = $this->options['method']; |
|---|
| 105 |
$results = $tableModel->$method($this->filterParameters($parameters)); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
// Doctrine_Collection then we need to create a new Doctrine_Collection with |
|---|
| 110 |
// one element inside and return that |
|---|
| 111 |
if ($results instanceof Doctrine_Record) |
|---|
| 112 |
{ |
|---|
| 113 |
$obj = $results; |
|---|
| 114 |
$results = new Doctrine_Collection($obj->getTable()); |
|---|
| 115 |
$results[] = $obj; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
return $results; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
protected function doConvertObjectToArray($object) |
|---|
| 122 |
{ |
|---|
| 123 |
if (isset($this->options['convert']) || method_exists($object, 'toParams')) |
|---|
| 124 |
{ |
|---|
| 125 |
return parent::doConvertObjectToArray($object); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
$parameters = array(); |
|---|
| 129 |
foreach ($this->getRealVariables() as $variable) |
|---|
| 130 |
{ |
|---|
| 131 |
try { |
|---|
| 132 |
$parameters[$variable] = $object->$variable; |
|---|
| 133 |
} catch (Exception $e) { |
|---|
| 134 |
try { |
|---|
| 135 |
$method = 'get'.sfInflector::camelize($variable); |
|---|
| 136 |
$parameters[$variable] = $object->$method(); |
|---|
| 137 |
} catch (Exception $e) {} |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
return $parameters; |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|