1 <?php
2 Yii::import('system.gii.generators.model.ModelCode');
3 Yii::import('ext.gtc.components.*');
4
5 class FullModelCode extends ModelCode
6 {
7 public $tables;
8 public $baseClass = 'CActiveRecord';
9 public $identificationColumn = null;
10 public $messageCatalog = 'crud';
11
12 public function init()
13 {
14 parent::init();
15
16 if (!@class_exists("CSaveRelationsBehavior")) {
17 throw new CException("Fatal Error: Class 'CSaveRelationsBehavior' could not be found in your application! Add 'ext.gtc.components.*' to your import paths.");
18 }
19
20 }
21
22 public function rules()
23 {
24 return array_merge(
25 parent::rules(),
26 array(
27 array('identificationColumn', 'safe'),
28 array('messageCatalog', 'match', 'pattern' => '/^[a-zA-Z_][\w.]*$/',
29 'message' => '{attribute} should only contain word characters.'),
30 ));
31 }
32
33 public function prepare()
34 {
35 parent::prepare();
36
37 $generate_whole_db = false;
38 $templatePath = $this->templatePath;
39
40 if (($pos = strrpos($this->tableName, '.')) !== false) {
41 $schema = substr($this->tableName, 0, $pos);
42 $tableName = substr($this->tableName, $pos + 1);
43 }
44 else {
45 $schema = '';
46 $tableName = $this->tableName;
47 }
48 if ($tableName[strlen($tableName) - 1] === '*') {
49 $generate_whole_db = true;
50 $this->tables = Yii::app()->{$this->connectionId}->schema->getTables($schema);
51 if ($this->tablePrefix != '') {
52 foreach ($this->tables as $i => $table) {
53 if (strpos($table->name, $this->tablePrefix) !== 0) {
54 unset($this->tables[$i]);
55 }
56 }
57 }
58 }
59 else {
60 $this->tables = array($this->getTableSchema($this->tableName));
61 }
62
63 $this->relations = $this->generateRelations();
64
65 foreach ($this->tables as $table) {
66 $tableName = $this->removePrefix($table->name);
67 $className = $this->generateClassName($table->name);
68
69 $params = array(
70 'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
71 'modelClass' => $className,
72 'columns' => $table->columns,
73 'labels' => $this->generateLabels($table),
74 'rules' => $this->generateRules($table),
75 'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
76 );
77
78 if ($this->template != 'singlefile') {
79 $this->files[] = new CCodeFile(
80 Yii::getPathOfAlias($this->modelPath) . '/' . 'Base' . $className . '.php',
81 $this->render($templatePath . '/basemodel.php', $params)
82 );
83 }
84 }
85 }
86
87 public function requiredTemplates()
88 {
89 if ($this->template == 'singlefile') {
90 return array('model.php');
91 }
92 else {
93 return array(
94 'model.php',
95 'basemodel.php',
96 );
97 }
98 }
99
100 public function getBehaviors($columns)
101 {
102 $behaviors = 'return array(';
103 if (count($this->relations) > 0) {
104 $behaviors .= "'CSaveRelationsBehavior', array(
105 'class' => 'CSaveRelationsBehavior'),";
106 }
107
108 foreach ($columns as $name => $column) {
109 if (in_array($column->name, array(
110 'create_time',
111 'createtime',
112 'created_at',
113 'createdat',
114 'changed',
115 'changed_at',
116 'updatetime',
117 'update_time',
118 'timestamp'))
119 ) {
120 $behaviors .= sprintf("\n\t\t'CTimestampBehavior' => array(
121 'class' => 'zii.behaviors.CTimestampBehavior',
122 'createAttribute' => %s,
123 'updateAttribute' => %s,
124 \t),\n", $this->getCreatetimeAttribute($columns),
125 $this->getUpdatetimeAttribute($columns));
126 break;
127 }
128 }
129 foreach ($columns as $name => $column) {
130 if (in_array($column->name, array(
131 'user_id',
132 'userid',
133 'ownerid',
134 'owner_id',
135 'created_by',
136 'createdby',
137 'create_user'))
138 ) {
139 $behaviors .= sprintf("\n\t\t'OwnerBehavior' => array(
140 'class' => 'OwnerBehavior',
141 'ownerColumn' => '%s',
142 \t),\n", $column->name);
143 break;
144
145 }
146 }
147
148
149 $behaviors .= "\n);\n";
150
151 return $behaviors;
152 }
153
154 public function generateRules($table)
155 {
156 $rules = array();
157 $required = array();
158 $null = array();
159 $integers = array();
160 $numerical = array();
161 $length = array();
162 $safe = array();
163 foreach ($table->columns as $column) {
164 if ($column->isPrimaryKey && $table->sequenceName !== null) {
165 continue;
166 }
167 $r = !$column->allowNull && $column->defaultValue === null;
168 if ($r) {
169 $required[] = $column->name;
170 }
171 else {
172 $null[] = $column->name;
173 }
174 if ($column->type === 'integer') {
175 $integers[] = $column->name;
176 }
177 else {
178 if ($column->type === 'double') {
179 $numerical[] = $column->name;
180 }
181 else {
182 if ($column->type === 'string' && $column->size > 0) {
183 $length[$column->size][] = $column->name;
184 if ($column->name == $this->identificationColumn) {
185 $rules[] = "array('{$column->name}', 'unique')";
186 $rules[] = "array('{$column->name}', 'identificationColumnValidator')";
187 }
188 }
189 else {
190 if (!$column->isPrimaryKey && !$r) {
191 $safe[] = $column->name;
192 }
193 }
194 }
195 }
196
197 }
198 if ($required !== array()) {
199 $rules[] = "array('" . implode(', ', $required) . "', 'required')";
200 }
201 if ($null !== array()) {
202 $rules[] = "array('" . implode(', ', $null) . "', 'default', 'setOnEmpty' => true, 'value' => null)";
203 }
204 if ($integers !== array()) {
205 $rules[] = "array('" . implode(', ', $integers) . "', 'numerical', 'integerOnly'=>true)";
206 }
207 if ($numerical !== array()) {
208 $rules[] = "array('" . implode(', ', $numerical) . "', 'numerical')";
209 }
210 if ($length !== array()) {
211 foreach ($length as $len => $cols)
212 $rules[] = "array('" . implode(', ', $cols) . "', 'length', 'max'=>$len)";
213 }
214 if ($safe !== array()) {
215 $rules[] = "array('" . implode(', ', $safe) . "', 'safe')";
216 }
217
218
219 return $rules;
220 }
221
222 function getCreatetimeAttribute($columns)
223 {
224 foreach (array('create_time', 'createtime', 'created_at', 'createdat', 'timestamp') as $try)
225 foreach ($columns as $column)
226 if ($try == $column->name) {
227 return sprintf("'%s'", $column->name);
228 }
229
230 return 'null';
231 }
232
233 function getUpdatetimeAttribute($columns)
234 {
235 foreach (array('update_time', 'updatetime', 'changed', 'changed_at') as $try)
236 foreach ($columns as $column)
237 if ($try == $column->name) {
238 return sprintf("'%s'", $column->name);
239 }
240
241 return 'null';
242 }
243
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
268
269 }
270
271 ?>
272