Development

HowToPatchTablesForI18nFromGeneratedSchema: sfPakePatchDbI18n.2.php

You must first sign up to be able to contribute.

HowToPatchTablesForI18nFromGeneratedSchema: sfPakePatchDbI18n.2.php

File sfPakePatchDbI18n.2.php, 4.6 kB (added by trisoft, 9 years ago)

add this attribute "baseClass: i18nBaseClass" to i18n tables

Line 
1 <?php
2 /**
3  * I18n reverse engineering database patch Pake task for schema.yml
4  *
5  * @author Klemen Slavič <klemen.slavic@tobonet.com>
6  * @author Tobonet spletni sistemi d.o.o.
7  * @version 2007-05-09
8  */
9
10 pake_desc('patch a database generated from DB to use i18n');
11 pake_task('patch-db-i18n');
12 pake_alias('patch', 'patch-db-i18n');
13
14
15 /**
16  * Patches the generated schema.yml file with the I18n `_attributes` parameters
17  *
18  * This Pake task assumes that `schema.yml` was generated using
19  * `propel-build-schema` from a preexisting database, for example,
20  * when DBDesigner was used to construct the database. It appends
21  * the necessary `_attributes` tags to database table definitions
22  * to provide bindings for I18n tables.
23  *
24  * This is only possible if the tables are named using suffixes:
25  *
26  * OK:
27  *    × `table`, `table_i18n`
28  *    × `table_a`, `table_a_smth`
29  * BAD:
30  *    × table, translated_table
31  *    × table, t_table_i18n
32  *
33  * If no suffix is povided within the second argument, the task assumes
34  * '_i18n'.
35  *
36  * Also assumes that the column name is `culture`. Will create the column
37  * if it does not exist or overwrite any existing configuration relevant
38  * to the i18n-ification.
39  *
40  * @param string $task
41  * @param array $args
42  */
43 function run_patch_db_i18n($task, $args) {
44     
45     if(count($args) < 1)
46         throw new Exception('Syntax is: "patch-db-i18n <application> [<suffix>  [<column>]]"');
47     
48     $app = $args[0];
49     
50     define('SF_ROOT_DIR',    sfConfig::get("sf_root_dir"));
51     define('SF_APP',         $app);
52     define('SF_ENVIRONMENT', 'dev');
53     define('SF_DEBUG',       false);
54     
55     $appDir = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP;
56     if(!is_dir($appDir))
57         throw new Exception("unknown application : $appDir");
58     
59     require_once($appDir.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
60     $context = sfContext::getInstance();
61     
62     if(!$context->getI18n())
63         throw new Exception('i18n not set in config/settings.yml');
64     
65     $suffix = isset($args[1])? $args[1]: "_i18n";
66     
67     $column = isset($args[2])? $args[2]: "culture";
68     
69     $verbose = pakeApp::get_instance()->get_verbose();
70     
71     if($verbose) echo '>> app    ' . pakeApp::excerpt('using application ' . $app) . "\n";
72     if($verbose) echo '>> parsing schema.yml' . "\n";
73
74     
75     // check for `schema.yml`
76     
77     $schema_path = sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'schema.yml';
78     if(!file_exists($schema_path))
79         throw new Exception("You must first generate schema.yml from the database!");
80     
81     $schema = sfYaml::load($schema_path);
82     
83     if($verbose) echo '>> finished parsing `schema.yml`' . "\n";
84     
85     if($verbose) echo '>> scanning for i18n tables' . "\n";
86
87     $table_names = array_keys($schema['propel']);
88     $pairs = array();
89     // find parent/child pairs
90     foreach($table_names as $name) {
91         if(in_array($name . $suffix, $table_names)) {
92             $pairs[] = array("parent" => $name, "child" => $name . $suffix);
93         }
94     }
95     
96     // list the tables
97     if($verbose) {
98         echo '>>     tables to patch:' . "\n";
99         foreach($pairs as $pair) {
100             echo '        - "' . $pair['parent'] . '" and "' . $pair['child'] . '"' . "\n";
101         }
102     }
103     
104     if($verbose) echo '>> appending i18n parameters to tables' . "\n";
105     
106     $parent = null;
107     $child = null;
108     // iterate through the matched tables and append necessary attributes
109     foreach($pairs as $pair) {
110         $parent =& $schema['propel'][$pair["parent"]];
111         $child =& $schema['propel'][$pair["child"]];
112         // set parent as I18n enabled
113         $parent['_attributes']['isI18n'] = 'true';
114         $parent['_attributes']['i18nTable'] = $pair['child'];
115         
116         //Radu Topala, radu.topala@trisoft.ro -- additional help
117         $parent['_attributes']['baseClass'] = 'i18nBaseClass';
118         
119         // set child table's culture column
120         if(!isset($child[$column]))
121             $child[$column] = array();
122         $child[$column]['isCulture'] = 'true';
123         /*
124         $child[$column]['type'] = 'varchar';
125         $child[$column]['size'] = 7;
126         $child[$column]['required'] = 'true';
127         $child[$column]['primaryKey'] = 'true';
128         */
129     }
130     
131     if($verbose) echo '>> finished appending, writing files' . "\n";
132     
133     // back up the current schema.yml
134     if(!copy($schema_path, $schema_path . '.bak'))
135         throw new Exception("Could not back up the original `schema.yml`!");
136     else
137         if($verbose) echo 'file+   ' . pakeApp::excerpt($schema_path . '.bak') . "\n";
138         
139     if(file_put_contents($schema_path, sfYaml::dump($schema)) === false)
140         throw new Exception("Could not write modified `schema.yml` file!");
141     else
142         if($verbose) echo 'file*   ' . pakeApp::excerpt($schema_path) . "\n";
143         
144     if($verbose) echo '>> finished modifying schema.yml' . "\n";
145     
146     echo 'Please run `propel-build-model` and `symfony cc` to generate the modified Propel classes.' . "\n";
147 }