Changeset 4682
- Timestamp:
- 07/13/08 01:14:08 (5 months ago)
- Location:
- branches/1.0
- Files:
-
- 9 modified
-
docs/manual/de/getting-started/working-with-existing-databases.txt (modified) (1 diff)
-
docs/manual/en/getting-started/working-with-existing-databases.txt (modified) (1 diff)
-
docs/manual/ja/getting-started.txt (modified) (1 diff)
-
docs/manual/ja/getting-started/working-with-existing-databases.txt (modified) (1 diff)
-
lib/Doctrine.php (modified) (1 diff)
-
lib/Doctrine/Configurable.php (modified) (1 diff)
-
lib/Doctrine/Import.php (modified) (4 diffs)
-
lib/Doctrine/Inflector.php (modified) (2 diffs)
-
tests/ImportTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0/docs/manual/de/getting-started/working-with-existing-databases.txt
r4674 r4682 97 97 98 98 This is where you can put your custom finder methods which can be used by calling Doctrine::getTable('User'). 99 100 +++ Singularizing Import101 102 By default Doctrine will attempt to ensure your models are singularized when they are imported from an existing103 database. This feature may not be ideal for everyone so it can easily be turned off using an attribute.104 105 <code type="php">106 Doctrine_Manager::getInstance()->setAttribute('singularize_import', false);107 </code>108 109 Now, if you have a table named "files", the result will be a model named "Files" instead of "File". -
branches/1.0/docs/manual/en/getting-started/working-with-existing-databases.txt
r4674 r4682 97 97 98 98 This is where you can put your custom finder methods which can be used by calling Doctrine::getTable('User'). 99 100 +++ Singularizing Import101 102 By default Doctrine will attempt to ensure your models are singularized when they are imported from an existing103 database. This feature may not be ideal for everyone so it can easily be turned off using an attribute.104 105 <code type="php">106 Doctrine_Manager::getInstance()->setAttribute('singularize_import', false);107 </code>108 109 Now, if you have a table named "files", the result will be a model named "Files" instead of "File". -
branches/1.0/docs/manual/ja/getting-started.txt
r4674 r4682 263 263 264 264 ããã«ã«ã¹ã¿ã ã¡ãœããã远å ããŠDoctrine::getTable('User')ã䜿ã£ãŠåŒã¶ããšãã§ããŸãã 265 266 +++ ã€ã³ããŒãã®éã®ããŒãã«åã®åæ°åœ¢å267 268 ããã©ã«ãã§ã¯Doctrineã¯æ¢åã®ããŒã¿ããŒã¹ããèªã¿èŸŒãŸããåã¢ãã«ã®ååãåæ°åã«ããããšããŸãããã®æ©èœãæãŸãªãæ¹ã¯ä»¥äžã®æ¹æ³ã§ç°¡åã«offã«ããŠãã ãã269 270 <code type="php">271 Doctrine_Manager::getInstance()->setAttribute('singularize_import', false);272 </code>273 274 ããã§"files"ãšããããŒãã«åããã¯"File.php"ãšããã¢ãã«ã¯ç¡ãã"Files.php"ãšããã¢ãã«ãçæãããŸãã275 265 276 266 ++ ããŒãã«ã®äœæ -
branches/1.0/docs/manual/ja/getting-started/working-with-existing-databases.txt
r4674 r4682 90 90 91 91 ããã«ã«ã¹ã¿ã ã¡ãœããã远å ããŠDoctrine::getTable('User')ã䜿ã£ãŠåŒã¶ããšãã§ããŸãã 92 93 +++ Singularizing Import94 95 ããã©ã«ãã§ã¯Doctrineã¯æ¢åã®ããŒã¿ããŒã¹ããèªã¿èŸŒãŸããåã¢ãã«ã®ååãåæ°åã«ããããšããŸãããã®æ©èœãæãŸãªãæ¹ã¯ä»¥äžã®æ¹æ³ã§ç°¡åã«offã«ããŠãã ãã96 97 <code type="php">98 Doctrine_Manager::getInstance()->setAttribute('singularize_import', false);99 </code>100 101 ããã§"files"ãšããããŒãã«åããã¯"File.php"ãšããã¢ãã«ã¯ç¡ãã"Files.php"ãšããã¢ãã«ãçæãããŸãã -
branches/1.0/lib/Doctrine.php
r4527 r4682 195 195 const ATTR_MODEL_LOADING = 161; 196 196 const ATTR_RECURSIVE_MERGE_FIXTURES = 162; 197 const ATTR_SINGULARIZE_IMPORT = 163;198 197 const ATTR_USE_DQL_CALLBACKS = 164; 199 198 -
branches/1.0/lib/Doctrine/Configurable.php
r4496 r4682 181 181 case Doctrine::ATTR_QUERY_CACHE_LIFESPAN: 182 182 case Doctrine::ATTR_RECURSIVE_MERGE_FIXTURES; 183 case Doctrine::ATTR_SINGULARIZE_IMPORT;184 183 case Doctrine::ATTR_USE_DQL_CALLBACKS; 185 184 -
branches/1.0/lib/Doctrine/Import.php
r4617 r4682 364 364 public function importSchema($directory, array $databases = array(), array $options = array()) 365 365 { 366 $options['singularize'] = ! isset($options['singularize']) ?367 $this->conn->getAttribute('singularize_import'):$options['singularize'];368 369 366 $connections = Doctrine_Manager::getInstance()->getConnections(); 370 367 … … 386 383 $definition['tableName'] = $table; 387 384 388 if( ! isset($options['singularize']) || $options['singularize'] !== false) { 389 $classTable = $this->_singularizeTableName($table); 390 } else { 391 $classTable = Doctrine_Inflector::tableize($table); 392 } 385 $classTable = Doctrine_Inflector::tableize($table); 393 386 394 387 $definition['className'] = Doctrine_Inflector::classify($classTable); … … 401 394 foreach ($relations as $relation) { 402 395 $table = $relation['table']; 403 if( ! isset($options['singularize']) || $options['singularize'] !== false) { 404 $relClassTable = $this->_singularizeTableName($table); 405 } else { 406 $relClassTable = Doctrine_Inflector::tableize($table); 407 } 396 $relClassTable = Doctrine_Inflector::tableize($table); 408 397 $class = Doctrine_Inflector::classify($relClassTable); 409 398 if (in_array($class, $classes)) { … … 433 422 return $classes; 434 423 } 435 436 /**437 * Singularize a table name438 *439 * @param string $tableName440 * @return $singularTableName441 */442 protected function _singularizeTableName($tableName)443 {444 $e = explode('_', Doctrine_Inflector::tableize($tableName));445 foreach ($e as $k => $v) {446 $e[$k] = Doctrine_Inflector::singularize($v);447 }448 return implode('_', $e);449 }450 424 } -
branches/1.0/lib/Doctrine/Inflector.php
r4554 r4682 39 39 { 40 40 /** 41 * Pluralize an English noun. Converts 'Category' to 'Categories'.42 *43 * @param string $word English noun to pluralize44 * @return string $word Plural noun45 */46 public static function pluralize($word)47 {48 $plural = array('/(quiz)$/i' => '\1zes',49 '/^(ox)$/i' => '\1en',50 '/([m|l])ouse$/i' => '\1ice',51 '/(matr|vert|ind)ix|ex$/i' => '\1ices',52 '/(x|ch|ss|sh)$/i' => '\1es',53 '/([^aeiouy]|qu)ies$/i' => '\1y',54 '/([^aeiouy]|qu)y$/i' => '\1ies',55 '/(hive)$/i' => '\1s',56 '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',57 '/sis$/i' => 'ses',58 '/([ti])um$/i' => '\1a',59 '/(buffal|tomat)o$/i' => '\1oes',60 '/(bu)s$/i' => '\1ses',61 '/(alias|status)/i' => '\1es',62 '/(octop|vir)us$/i' => '\1i',63 '/(ax|test)is$/i' => '\1es',64 '/s$/i' => 's',65 '/$/' => 's');66 67 $uncountable = array('equipment',68 'information',69 'rice',70 'money',71 'species',72 'series',73 'fish',74 'sheep');75 76 $irregular = array('person' => 'people',77 'man' => 'men',78 'child' => 'children',79 'sex' => 'sexes',80 'move' => 'moves');81 82 $lowercasedWord = strtolower($word);83 84 foreach ($uncountable as $_uncountable) {85 if(substr($lowercasedWord, (-1 * strlen($_uncountable))) == $_uncountable) {86 return $word;87 }88 }89 90 foreach ($irregular as $_plural=> $_singular){91 if (preg_match('/('.$_plural.')$/i', $word, $arr)) {92 return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1) . substr($_singular,1), $word);93 }94 }95 96 foreach ($plural as $rule => $replacement) {97 if (preg_match($rule, $word)) {98 return preg_replace($rule, $replacement, $word);99 }100 }101 102 return false;103 }104 105 /**106 * Singularize an English noun. Converts 'Quizzes' to 'Quiz'.107 *108 * @param string $word English noun to singularize109 * @return string $word Singular noun.110 */111 public static function singularize($word)112 {113 $singular = array('/(quiz)zes$/i' => '\\1',114 '/(matr)ices$/i' => '\\1ix',115 '/(vert|ind)ices$/i' => '\\1ex',116 '/^(ox)en/i' => '\\1',117 '/(alias|status)es$/i' => '\\1',118 '/([octop|vir])i$/i' => '\\1us',119 '/(cris|ax|test)es$/i' => '\\1is',120 '/(shoe)s$/i' => '\\1',121 '/(o)es$/i' => '\\1',122 '/(bus)es$/i' => '\\1',123 '/([m|l])ice$/i' => '\\1ouse',124 '/(x|ch|ss|sh)es$/i' => '\\1',125 '/(m)ovies$/i' => '\\1ovie',126 '/(s)eries$/i' => '\\1eries',127 '/([^aeiouy]|qu)ies$/i' => '\\1y',128 '/([lr])ves$/i' => '\\1f',129 '/(tive)s$/i' => '\\1',130 '/(hive)s$/i' => '\\1',131 '/([^f])ves$/i' => '\\1fe',132 '/(^analy)ses$/i' => '\\1sis',133 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis',134 '/([ti])a$/i' => '\\1um',135 '/(n)ews$/i' => '\\1ews',136 '/^(.{2,2})$/i' => '\\1',137 '/s$/i' => '');138 139 $uncountable = array('equipment',140 'information',141 'rice',142 'money',143 'species',144 'series',145 'fish',146 'sheep',147 'sms',148 'status',149 'access');150 151 $irregular = array('person' => 'people',152 'man' => 'men',153 'child' => 'children',154 'sex' => 'sexes',155 'move' => 'moves');156 157 $lowercasedWord = strtolower($word);158 foreach ($uncountable as $_uncountable){159 if(substr($lowercasedWord, ( -1 * strlen($_uncountable))) == $_uncountable){160 return $word;161 }162 }163 164 foreach ($irregular as $_singular => $_plural) {165 if (preg_match('/('.$_plural.')$/i', $word, $arr)) {166 return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);167 }168 }169 170 foreach ($singular as $rule => $replacement) {171 if (preg_match($rule, $word)) {172 return preg_replace($rule, $replacement, $word);173 }174 }175 176 return $word;177 }178 179 /**180 41 * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name' 181 42 * … … 185 46 public static function tableize($word) 186 47 { 187 // Would prefer this but it breaks unit tests. Forces the table underscore pattern188 // return self::pluralize(self::underscore($name));189 48 return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word)); 190 49 } -
branches/1.0/tests/ImportTestCase.php
r4317 r4682 52 52 Doctrine_Lib::removeDirectories('Import/_files'); 53 53 } 54 55 public function testImportSingularizeOn()56 {57 $this->dbh = new PDO('sqlite::memory:');58 59 $this->dbh->exec('CREATE TABLE imports_tests_users (id INTEGER PRIMARY KEY, name TEXT)');60 61 $this->conn = Doctrine_Manager::connection($this->dbh, 'tmp1234');62 $this->conn->setAttribute(Doctrine::ATTR_SINGULARIZE_IMPORT, true);63 $this->conn->import->importSchema('Import/_files', array('tmp1234'));64 65 $this->assertTrue(file_exists('Import/_files/ImportTestUser.php'));66 $this->assertTrue(file_exists('Import/_files/generated/BaseImportTestUser.php'));67 Doctrine_Lib::removeDirectories('Import/_files');68 }69 70 public function testImportSingularizeOff()71 {72 $this->dbh = new PDO('sqlite::memory:');73 74 $this->dbh->exec('CREATE TABLE imports_tests_users (id INTEGER PRIMARY KEY, name TEXT)');75 76 $this->conn = Doctrine_Manager::connection($this->dbh, 'tmp1235');77 $this->conn->setAttribute(Doctrine::ATTR_SINGULARIZE_IMPORT, false);78 $this->conn->import->importSchema('Import/_files', array('tmp1235'));79 80 $this->assertTrue(file_exists('Import/_files/ImportsTestsUsers.php'));81 $this->assertTrue(file_exists('Import/_files/generated/BaseImportsTestsUsers.php'));82 Doctrine_Lib::removeDirectories('Import/_files');83 }84 54 }