Development

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

You must first sign up to be able to contribute.

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

Revision 19987, 5.7 kB (checked in by Jonathan.Wage, 5 years ago)

[1.2, 1.3] Fixing issue with magic accessors/getters when using camel case with relationships (closes #6178)

  • Property svn:keywords set to Id Date Author Revision HeadURL
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    * Custom Doctrine_Record constructor.
30    * Used to initialize I18n to make sure the culture is set from symfony
31    *
32    * @return void
33    */
34   public function construct()
35   {
36     self::initializeI18n();
37
38     if ($this->getTable()->hasRelation('Translation'))
39     {
40       $this->unshiftFilter(new sfDoctrineRecordI18nFilter());
41     }
42   }
43
44   /**
45    * Initialize I18n culture from symfony sfUser instance
46    * Add event listener to change default culture whenever the user changes culture
47    *
48    * @return void
49    */
50   public static function initializeI18n()
51   {
52     if (!self::$_initialized)
53     {
54       if (!self::$_initialized && class_exists('sfProjectConfiguration', false))
55       {
56         $dispatcher = sfProjectConfiguration::getActive()->getEventDispatcher();
57         $dispatcher->connect('user.change_culture', array('sfDoctrineRecord', 'listenToChangeCultureEvent'));
58       }
59
60       if (class_exists('sfContext', false) && sfContext::hasInstance() && $user = sfContext::getInstance()->getUser())
61       {
62         self::$_defaultCulture = $user->getCulture();
63       }
64       self::$_initialized = true;
65     }
66   }
67
68   /**
69    * Listens to the user.change_culture event.
70    *
71    * @param sfEvent An sfEvent instance
72    */
73   public static function listenToChangeCultureEvent(sfEvent $event)
74   {
75     self::$_defaultCulture = $event['culture'];
76   }
77
78   /**
79    * Sets the default culture
80    *
81    * @param string $culture
82    */
83   static public function setDefaultCulture($culture)
84   {
85     self::$_defaultCulture = $culture;
86   }
87
88   /**
89    * Return the default culture
90    *
91    * @return string the default culture
92    */
93   static public function getDefaultCulture()
94   {
95     self::initializeI18n();
96
97     if (!self::$_defaultCulture)
98     {
99       throw new sfException('The default culture has not been set');
100     }
101     return self::$_defaultCulture;
102   }
103
104   /**
105    * Get the primary key of a Doctrine_Record.
106    * This a proxy method to Doctrine_Record::identifier() for Propel BC
107    *
108    * @return mixed $identifier Array for composite primary keys and string for single primary key
109    */
110   public function getPrimaryKey()
111   {
112     $identifier = (array) $this->identifier();
113     return end($identifier);
114   }
115
116   /**
117    * Function require by symfony >= 1.2 admin generators
118    *
119    * @return boolean
120    */
121   public function isNew()
122   {
123     return ! $this->exists();
124   }
125
126   /**
127    * Returns a string representation of the record.
128    *
129    * @return string A string representation of the record.
130    */
131   public function __toString()
132   {
133     $guesses = array('name',
134                      'title',
135                      'description',
136                      'subject',
137                      'keywords',
138                      'id');
139
140     // we try to guess a column which would give a good description of the object
141     foreach ($guesses as $descriptionColumn)
142     {
143       try
144       {
145         return (string) $this->get($descriptionColumn);
146       } catch (Exception $e) {}
147     }
148
149     return sprintf('No description for object of class "%s"', $this->getTable()->getComponentName());
150   }
151
152   /*
153    * Provide accessors with setters and getters to Doctrine models.
154    *
155    * @param  string $method     The method name.
156    * @param  array  $arguments  The method arguments.
157    * @return mixed The returned value of the called method.
158    */
159   public function __call($method, $arguments)
160   {
161     try {
162       if (in_array($verb = substr($method, 0, 3), array('set', 'get')))
163       {
164         $name = substr($method, 3);
165
166         $table = $this->getTable();
167         if ($table->hasRelation($name))
168         {
169           $entityName = $name;
170         }
171         else if ($table->hasField($fieldName = $table->getFieldName($name)))
172         {
173           $entityNameLower = strtolower($fieldName);
174           if ($table->hasField($entityNameLower) || $table->hasRelation($entityNameLower))
175           {
176             $entityName = $entityNameLower;
177           } else {
178             $entityName = $fieldName;
179           }
180         }
181         else
182         {
183           $underScored = $table->getFieldName(sfInflector::underscore($name));
184           if ($table->hasField($underScored) || $table->hasRelation($underScored))
185           {
186             $entityName = $underScored;
187           } else if ($table->hasField(strtolower($name)) || $table->hasRelation(strtolower($name))) {
188             $entityName = strtolower($name);
189           } else {
190             $camelCase = $table->getFieldName(sfInflector::camelize($name));
191             $camelCase = strtolower($camelCase[0]).substr($camelCase, 1, strlen($camelCase));
192             if ($table->hasField($camelCase) || $table->hasRelation($camelCase))
193             {
194               $entityName = $camelCase;
195             } else {
196               $entityName = $underScored;
197             }
198           }
199         }
200
201         return call_user_func_array(
202           array($this, $verb),
203           array_merge(array($entityName), $arguments)
204         );
205       } else {
206         return parent::__call($method, $arguments);
207       }
208     } catch(Exception $e) {
209       return parent::__call($method, $arguments);
210     }
211   }
212 }
Note: See TracBrowser for help on using the browser.