Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/routing/sfDoctrineRoute.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/routing/sfDoctrineRoute.class.php

Revision 24498, 4.3 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3, 1.4] fixed getObjectsForParameters() failing on second invocation on sfDoctrineRoute.class.php (fixes #7716)

Line 
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 11475 2008-09-12 11:07:23Z fabien $
22  */
23 class sfDoctrineRoute extends sfObjectRoute
24 {
25   protected
26     $query = null;
27
28   /**
29    * Constructor.
30    *
31    * @param string $pattern       The pattern to match
32    * @param array  $defaults      An array of default parameter values
33    * @param array  $requirements  An array of requirements for parameters (regexes)
34    * @param array  $options       An array of options
35    *
36    * @see sfObjectRoute
37    */
38   public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
39   {
40     parent::__construct($pattern, $defaults, $requirements, $options);
41
42     $this->options['object_model'] = $this->options['model'];
43   }
44
45   public function setListQuery(Doctrine_Query $query)
46   {
47     if (!$this->isBound())
48     {
49       throw new LogicException('The route is not bound.');
50     }
51
52     $this->query = $query;
53   }
54
55   protected function getObjectForParameters($parameters)
56   {
57     $results = $this->getObjectsForParameters($parameters);
58
59     // If query returned Doctrine_Collection with results inside then we
60     // need to return the first Doctrine_Record
61     if ($results instanceof Doctrine_Collection)
62     {
63       if (count($results))
64       {
65         $results = $results->getFirst();
66       } else {
67         $results = null;
68       }
69     }
70     // If an object is returned then lets return it otherwise return null
71     else if(!is_object($results))
72     {
73       $results = null;
74     }
75
76     return $results;
77   }
78
79   protected function getObjectsForParameters($parameters)
80   {
81     $this->options['model'] = Doctrine::getTable($this->options['object_model']);
82
83     $variables = array();
84     $values = array();
85     foreach($this->getRealVariables() as $variable)
86     {
87       if($this->options['model']->hasColumn($this->options['model']->getColumnName($variable)))
88       {
89         $variables[] = $variable;
90         $values[$variable] = $parameters[$variable];
91       }
92     }
93
94     if (!isset($this->options['method']))
95     {
96       if (is_null($this->query))
97       {
98         $q = $this->options['model']->createQuery('a');
99         foreach ($values as $variable => $value)
100         {
101           $fieldName = $this->options['model']->getFieldName($variable);
102           $q->andWhere('a.'. $fieldName . ' = ?', $parameters[$variable]);
103         }
104       }
105       else
106       {
107         $q = $this->query;
108       }
109       if (isset($this->options['method_for_query']))
110       {
111         $method = $this->options['method_for_query'];
112         $results = $this->options['model']->$method($q);
113       }
114       else
115       {
116         $results = $q->execute();
117       }
118     }
119     else
120     {
121       $method = $this->options['method'];
122       $results = $this->options['model']->$method($this->filterParameters($parameters));
123     }
124
125     // If query returned a Doctrine_Record instance instead of a
126     // Doctrine_Collection then we need to create a new Doctrine_Collection with
127     // one element inside and return that
128     if ($results instanceof Doctrine_Record)
129     {
130       $obj = $results;
131       $results = new Doctrine_Collection($obj->getTable());
132       $results[] = $obj;
133     }
134
135     return $results;
136   }
137
138   protected function doConvertObjectToArray($object)
139   {
140     if (isset($this->options['convert']) || method_exists($object, 'toParams'))
141     {
142       return parent::doConvertObjectToArray($object);
143     }
144
145     $parameters = array();
146     foreach ($this->getRealVariables() as $variable)
147     {
148       try {
149         $parameters[$variable] = $object->$variable;
150       } catch (Exception $e) {
151         try {
152           $method = 'get'.sfInflector::camelize($variable);
153           $parameters[$variable] = $object->$method;
154         } catch (Exception $e) {}
155       }
156     }
157
158     return $parameters;
159   }
160 }
Note: See TracBrowser for help on using the browser.