Changeset 4682

Show
Ignore:
Timestamp:
07/13/08 01:14:08 (5 months ago)
Author:
jwage
Message:

fixes #1186 - removing all use of singularization and plurilization

Location:
branches/1.0
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/docs/manual/de/getting-started/working-with-existing-databases.txt

    r4674 r4682  
    9797 
    9898This is where you can put your custom finder methods which can be used by calling Doctrine::getTable('User'). 
    99  
    100 +++ Singularizing Import 
    101  
    102 By default Doctrine will attempt to ensure your models are singularized when they are imported from an existing  
    103 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  
    9797 
    9898This is where you can put your custom finder methods which can be used by calling Doctrine::getTable('User'). 
    99  
    100 +++ Singularizing Import 
    101  
    102 By default Doctrine will attempt to ensure your models are singularized when they are imported from an existing  
    103 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  
    263263 
    264264ここにカスタムメ゜ッドを远加しお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"ずいうモデルが生成されたす。 
    275265 
    276266++ テヌブルの䜜成 
  • branches/1.0/docs/manual/ja/getting-started/working-with-existing-databases.txt

    r4674 r4682  
    9090 
    9191ここにカスタムメ゜ッドを远加しおDoctrine::getTable('User')を䜿っお呌ぶこずができたす。 
    92  
    93 +++ Singularizing Import 
    94  
    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  
    195195    const ATTR_MODEL_LOADING            = 161; 
    196196    const ATTR_RECURSIVE_MERGE_FIXTURES = 162; 
    197     const ATTR_SINGULARIZE_IMPORT       = 163; 
    198197    const ATTR_USE_DQL_CALLBACKS        = 164; 
    199198 
  • branches/1.0/lib/Doctrine/Configurable.php

    r4496 r4682  
    181181            case Doctrine::ATTR_QUERY_CACHE_LIFESPAN: 
    182182            case Doctrine::ATTR_RECURSIVE_MERGE_FIXTURES; 
    183             case Doctrine::ATTR_SINGULARIZE_IMPORT; 
    184183            case Doctrine::ATTR_USE_DQL_CALLBACKS; 
    185184 
  • branches/1.0/lib/Doctrine/Import.php

    r4617 r4682  
    364364    public function importSchema($directory, array $databases = array(), array $options = array()) 
    365365    { 
    366         $options['singularize'] = ! isset($options['singularize']) ?  
    367                 $this->conn->getAttribute('singularize_import'):$options['singularize']; 
    368  
    369366        $connections = Doctrine_Manager::getInstance()->getConnections(); 
    370367 
     
    386383              $definition['tableName'] = $table; 
    387384 
    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); 
    393386 
    394387              $definition['className'] = Doctrine_Inflector::classify($classTable); 
     
    401394                  foreach ($relations as $relation) { 
    402395                      $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); 
    408397                      $class = Doctrine_Inflector::classify($relClassTable); 
    409398                      if (in_array($class, $classes)) { 
     
    433422        return $classes; 
    434423    } 
    435  
    436     /** 
    437      * Singularize a table name 
    438      * 
    439      * @param string $tableName  
    440      * @return $singularTableName 
    441      */ 
    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     } 
    450424} 
  • branches/1.0/lib/Doctrine/Inflector.php

    r4554 r4682  
    3939{ 
    4040    /** 
    41      * Pluralize an English noun. Converts 'Category' to 'Categories'. 
    42      * 
    43      * @param    string $word English noun to pluralize 
    44      * @return   string $word Plural noun 
    45      */ 
    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 singularize 
    109      * @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     /** 
    18041     * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name' 
    18142     * 
     
    18546    public static function tableize($word) 
    18647    { 
    187         // Would prefer this but it breaks unit tests. Forces the table underscore pattern 
    188         // return self::pluralize(self::underscore($name)); 
    18948        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word)); 
    19049    } 
  • branches/1.0/tests/ImportTestCase.php

    r4317 r4682  
    5252        Doctrine_Lib::removeDirectories('Import/_files'); 
    5353    } 
    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     } 
    8454}