Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/record/sfDoctrineRecord.class.php

Revision 24606, 7.1 kB (checked in by Jonathan.Wage, 3 years ago)

[1.3, 1.4] Fixing sfDoctrineRecord::call() so proper exception is thrown (closes #7212)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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  * Base sfDoctrineRecord extends the base Doctrine_Record in Doctrine to provide some
14  * symfony specific functionality to Doctrine_Records
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @author     Jonathan H. Wage <jonwage@gmail.com>
20  * @version    SVN: $Id$
21  */
22 abstract class sfDoctrineRecord extends Doctrine_Record
23 {
24   static protected
25     $_initialized    = false,
26     $_defaultCulture = 'en';
27
28   /**
29    * Initializes internationalization.
30    *
31    * @see Doctrine_Record
32    */
33   public function construct()
34   {
35     if ($this->getTable()->hasRelation('Translation'))
36     {
37       self::initializeI18n();
38
39       // only add filter to each table once
40       if (!$this->getTable()->getOption('has_symfony_i18n_filter'))
41       {
42         $this->getTable()
43           ->unshiftFilter(new sfDoctrineRecordI18nFilter())
44           ->setOption('has_symfony_i18n_filter', true)
45         ;
46       }
47     }
48   }
49
50   /**
51    * Initializes internationalization.
52    */
53   static public function initializeI18n()
54   {
55     if (!self::$_initialized)
56     {
57       $dispatcher = sfProjectConfiguration::getActive()->getEventDispatcher();
58       $dispatcher->connect('user.change_culture', array('sfDoctrineRecord', 'listenToChangeCultureEvent'));
59
60       if (sfContext::hasInstance() && $user = sfContext::getInstance()->getUser())
61       {
62         self::$_defaultCulture = $user->getCulture();
63       }
64
65       self::$_initialized = true;
66     }
67   }
68
69   /**
70    * Listens to the user.change_culture event.
71    *
72    * @param sfEvent An sfEvent instance
73    */
74   static public function listenToChangeCultureEvent(sfEvent $event)
75   {
76     self::$_defaultCulture = $event['culture'];
77   }
78
79   /**
80    * Sets the default culture
81    *
82    * @param string $culture
83    */
84   static public function setDefaultCulture($culture)
85   {
86     self::initializeI18n();
87
88     self::$_defaultCulture = $culture;
89   }
90
91   /**
92    * Return the default culture
93    *
94    * @return string the default culture
95    */
96   static public function getDefaultCulture()
97   {
98     self::initializeI18n();
99
100     if (!self::$_defaultCulture)
101     {
102       throw new sfException('The default culture has not been set');
103     }
104
105     return self::$_defaultCulture;
106   }
107
108   /**
109    * Returns the current record's primary key.
110    *
111    * This a proxy method to {@link Doctrine_Record::identifier()} for
112    * compatibility with a Propel-style API.
113    *
114    * @return mixed The value of the current model's last identifier column
115    */
116   public function getPrimaryKey()
117   {
118     $identifier = (array) $this->identifier();
119     return end($identifier);
120   }
121
122   /**
123    * Function require by symfony >= 1.2 admin generators.
124    *
125    * @return boolean
126    */
127   public function isNew()
128   {
129     return ! $this->exists();
130   }
131
132   /**
133    * Returns a string representation of the record.
134    *
135    * @return string A string representation of the record
136    */
137   public function __toString()
138   {
139     $guesses = array('name',
140                      'title',
141                      'description',
142                      'subject',
143                      'keywords',
144                      'id');
145
146     // we try to guess a column which would give a good description of the object
147     foreach ($guesses as $descriptionColumn)
148     {
149       try
150       {
151         return (string) $this->get($descriptionColumn);
152       } catch (Exception $e) {}
153     }
154
155     return sprintf('No description for object of class "%s"', $this->getTable()->getComponentName());
156   }
157
158   /**
159    * Provides getter and setter methods.
160    *
161    * @param  string $method    The method name
162    * @param  array  $arguments The method arguments
163    *
164    * @return mixed The returned value of the called method
165    */
166   public function __call($method, $arguments)
167   {
168     $failed = false;
169     try {
170       if (in_array($verb = substr($method, 0, 3), array('set', 'get')))
171       {
172         $name = substr($method, 3);
173
174         $table = $this->getTable();
175         if ($table->hasRelation($name))
176         {
177           $entityName = $name;
178         }
179         else if ($table->hasField($fieldName = $table->getFieldName($name)))
180         {
181           $entityNameLower = strtolower($fieldName);
182           if ($table->hasField($entityNameLower))
183           {
184             $entityName = $entityNameLower;
185           } else {
186             $entityName = $fieldName;
187           }
188         }
189         else
190         {
191           $underScored = $table->getFieldName(sfInflector::underscore($name));
192           if ($table->hasField($underScored) || $table->hasRelation($underScored))
193           {
194             $entityName = $underScored;
195           } else if ($table->hasField(strtolower($name)) || $table->hasRelation(strtolower($name))) {
196             $entityName = strtolower($name);
197           } else {
198             $camelCase = $table->getFieldName(sfInflector::camelize($name));
199             $camelCase = strtolower($camelCase[0]).substr($camelCase, 1, strlen($camelCase));
200             if ($table->hasField($camelCase) || $table->hasRelation($camelCase))
201             {
202               $entityName = $camelCase;
203             } else {
204               $entityName = $underScored;
205             }
206           }
207         }
208
209         return call_user_func_array(
210           array($this, $verb),
211           array_merge(array($entityName), $arguments)
212         );
213       } else {
214         $failed = true;
215       }
216     } catch (Exception $e) {
217       $failed = true;
218     }
219     if ($failed)
220     {
221       try
222       {
223         return parent::__call($method, $arguments);
224       } catch (Doctrine_Record_UnknownPropertyException $e2) {}
225
226       if ($e)
227       {
228         throw $e;
229       } else if ($e2) {
230         throw $e2;
231       }
232     }
233   }
234
235   /**
236    * Get the Doctrine date value as a PHP DateTime object
237    *
238    * @param string $dateFieldName   The field name to get the DateTime object for
239    * @return DateTime $dateTime     The instance of PHPs DateTime
240    */
241   public function getDateTimeObject($dateFieldName)
242   {
243     $type = $this->getTable()->getTypeOf($dateFieldName);
244     if ($type == 'date' || $type == 'timestamp')
245     {
246       return new DateTime($this->get($dateFieldName));
247     }
248     else
249     {
250       throw new sfException('Cannot call getDateTimeObject() on a field that is not of type date or timestamp.');
251     }
252   }
253
254   /**
255    * Set the Doctrine date value by passing a valid PHP DateTime object instance
256    *
257    * @param string $dateFieldName       The field name to set the date for
258    * @param DateTime $dateTimeObject    The DateTime instance to use to set the value
259    * @return void
260    */
261   public function setDateTimeObject($dateFieldName, DateTime $dateTimeObject)
262   {
263     $type = $this->getTable()->getTypeOf($dateFieldName);
264     if ($type == 'date' || $type == 'timestamp')
265     {
266       return $this->set($dateFieldName, $dateTimeObject->format('Y-m-d H:i:s'));
267     }
268     else
269     {
270       throw new sfException('Cannot call setDateTimeObject() on a field that is not of type date or timestamp.');
271     }
272   }
273 }
Note: See TracBrowser for help on using the browser.