| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
* (c) Jonathan H. Wage <jonwage@gmail.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* sfDoctrineRoute represents a route that is bound to a Doctrine class.
|
|
|
14 |
*
|
|
|
15 |
* A Doctrine route can represent a single Doctrine object or a list of objects.
|
|
|
16 |
*
|
|
|
17 |
* @package symfony
|
|
|
18 |
* @subpackage doctrine
|
|
|
19 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
20 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
21 |
* @version SVN: $Id: sfDoctrineRoute.class.php 28633 2010-03-20 14:35:57Z Kris.Wallsmith $
|
|
|
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 |
// If query returned Doctrine_Collection with results inside then we
|
|
|
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 |
// If an object is returned then lets return it otherwise return null
|
|
|
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 |
// If query returned a Doctrine_Record instance instead of a
|
|
|
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 |
}
|