Development

HowToPatchTablesForI18nFromGeneratedSchema: doPatchDbI18nTask.class.php

You must first sign up to be able to contribute.

HowToPatchTablesForI18nFromGeneratedSchema: doPatchDbI18nTask.class.php

File doPatchDbI18nTask.class.php, 3.3 kB (added by patrickespake, 9 years ago)

Pluggin turned into a task to work with the symfony 1.1

Line 
1 <?php
2
3 class doPatchDbI18nTask extends sfBaseTask
4 {
5   protected function configure()
6   {
7     $this->namespace        = 'project';
8     $this->name             = 'patch-db-i18n';
9     $this->briefDescription = 'Patch a database generated from DB to use i18n';
10     $this->detailedDescription = <<<EOF
11 The [project:patch-db-i18n|INFO] task does things.
12 Call it with:
13
14   [php symfony project:patch-db-i18n|INFO]
15 EOF;
16   }
17
18   protected function execute($arguments = array(), $options = array())
19   {
20     define('SF_ROOT_DIR',    sfConfig::get("sf_root_dir"));
21     define('SF_ENVIRONMENT', 'dev');
22     define('SF_DEBUG',       false);
23
24     $suffix = isset($arguments[0]) ? $arguments[0]: "_i18n";
25     $column = isset($arguments[1]) ? $arguments[1]: "culture";
26
27     $this->logSection('patch-db-i18n', 'Parsing schema.yml');
28
29     // check for `schema.yml`
30     $schema_path = sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'schema.yml';
31
32     if(!file_exists($schema_path))
33       throw new Exception("You must first generate schema.yml from the database");
34
35     $schema = sfYaml::load($schema_path);
36
37     $this->logSection('patch-db-i18n', 'Finished parsing `schema.yml`');
38     $this->logSection('patch-db-i18n', 'Scanning for I18n tables');
39
40     $table_names = array_keys($schema['propel']);
41     $pairs = array();
42
43     // find parent/child pairs
44     foreach($table_names as $name)
45     {
46       if(in_array($name . $suffix, $table_names))
47         $pairs[] = array("parent" => $name, "child" => $name . $suffix);
48     }
49
50     // list the tables
51     $this->logSection('patch-db-i18n', 'Tables to patch:');
52     foreach($pairs as $pair)
53       $this->logSection('patch-db-i18n', '        - "' . $pair['parent'] . '" and "' . $pair['child']);
54
55     $this->logSection('patch-db-i18n', 'Appending i18n parameters to tables');
56
57     $parent = null;
58     $child = null;
59
60     // iterate through the matched tables and append necessary attributes
61     foreach($pairs as $pair)
62     {
63       $parent =& $schema['propel'][$pair["parent"]];
64       $child =& $schema['propel'][$pair["child"]];
65
66       // set parent as I18n enabled
67       $parent['_attributes']['isI18n'] = 'true';
68       $parent['_attributes']['i18nTable'] = $pair['child'];
69
70       // set child table's culture column
71       if(!isset($child[$column]))
72         $child[$column] = array();
73
74       $child[$column]['isCulture'] = 'true';
75
76       /*
77       $child[$column]['type'] = 'varchar';
78       $child[$column]['size'] = 7;
79       $child[$column]['required'] = 'true';
80       $child[$column]['primaryKey'] = 'true';
81       */
82     }
83
84     $this->logSection('patch-db-i18n', 'Finished appending, writing files');
85
86     // back up the current schema.yml
87     if(!copy($schema_path, $schema_path . '.bak'))
88       throw new Exception("Could not back up the original `schema.yml`");
89     else
90       $this->logSection('patch-db-i18n', 'File+   ' . $schema_path . '.bak');
91
92     if(file_put_contents($schema_path, sfYaml::dump($schema)) === false)
93       throw new Exception("Could not write modified `schema.yml` file!");
94     else
95       $this->logSection('patch-db-i18n', 'File*   ' . $schema_path);
96
97     $this->logSection('patch-db-i18n', 'Finished modifying schema.yml');
98     $this->logSection('patch-db-i18n', 'Please run `propel-build-model` and `symfony cc` to generate the modified Propel classes');
99   }
100 }
101