pictcode / lib / Cake / Console / Templates / default / classes / model.ctp @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (5.616 KB)
1 | 635eef61 | spyder1211 | <?php |
---|---|---|---|
2 | /** |
||
3 | * Model template file. |
||
4 | * |
||
5 | * Used by bake to create new Model files. |
||
6 | * |
||
7 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
||
8 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
||
9 | * |
||
10 | * Licensed under The MIT License |
||
11 | * For full copyright and license information, please see the LICENSE.txt |
||
12 | * Redistributions of files must retain the above copyright notice. |
||
13 | * |
||
14 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
||
15 | * @link http://cakephp.org CakePHP(tm) Project |
||
16 | * @package Cake.Console.Templates.default.classes |
||
17 | * @since CakePHP(tm) v 1.3 |
||
18 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
||
19 | */ |
||
20 | |||
21 | echo "<?php\n"; |
||
22 | echo "App::uses('{$plugin}AppModel', '{$pluginPath}Model');\n"; |
||
23 | ?> |
||
24 | /** |
||
25 | * <?php echo $name ?> Model |
||
26 | * |
||
27 | <?php |
||
28 | foreach (array('hasOne', 'belongsTo', 'hasMany', 'hasAndBelongsToMany') as $assocType) { |
||
29 | if (!empty($associations[$assocType])) { |
||
30 | foreach ($associations[$assocType] as $relation) { |
||
31 | echo " * @property {$relation['className']} \${$relation['alias']}\n"; |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | ?> |
||
36 | */ |
||
37 | class <?php echo $name ?> extends <?php echo $plugin; ?>AppModel { |
||
38 | |||
39 | <?php if ($useDbConfig !== 'default'): ?> |
||
40 | /** |
||
41 | * Use database config |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | public $useDbConfig = '<?php echo $useDbConfig; ?>'; |
||
46 | |||
47 | <?php endif; |
||
48 | |||
49 | if ($useTable && $useTable !== Inflector::tableize($name)): |
||
50 | $table = "'$useTable'"; |
||
51 | echo "/**\n * Use table\n *\n * @var mixed False or table name\n */\n"; |
||
52 | echo "\tpublic \$useTable = $table;\n\n"; |
||
53 | endif; |
||
54 | |||
55 | if ($primaryKey !== 'id'): ?> |
||
56 | /** |
||
57 | * Primary key field |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | public $primaryKey = '<?php echo $primaryKey; ?>'; |
||
62 | |||
63 | <?php endif; |
||
64 | |||
65 | if ($displayField): ?> |
||
66 | /** |
||
67 | * Display field |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $displayField = '<?php echo $displayField; ?>'; |
||
72 | |||
73 | <?php endif; |
||
74 | |||
75 | if (!empty($actsAs)): ?> |
||
76 | /** |
||
77 | * Behaviors |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | public $actsAs = array(<?php echo "\n\t"; foreach ($actsAs as $behavior): echo "\t"; var_export($behavior); echo ",\n\t"; endforeach; ?>); |
||
82 | |||
83 | <?php endif; |
||
84 | |||
85 | if (!empty($validate)): |
||
86 | echo "/**\n * Validation rules\n *\n * @var array\n */\n"; |
||
87 | echo "\tpublic \$validate = array(\n"; |
||
88 | foreach ($validate as $field => $validations): |
||
89 | echo "\t\t'$field' => array(\n"; |
||
90 | foreach ($validations as $key => $validator): |
||
91 | echo "\t\t\t'$key' => array(\n"; |
||
92 | echo "\t\t\t\t'rule' => array('$validator'),\n"; |
||
93 | echo "\t\t\t\t//'message' => 'Your custom message here',\n"; |
||
94 | echo "\t\t\t\t//'allowEmpty' => false,\n"; |
||
95 | echo "\t\t\t\t//'required' => false,\n"; |
||
96 | echo "\t\t\t\t//'last' => false, // Stop validation after this rule\n"; |
||
97 | echo "\t\t\t\t//'on' => 'create', // Limit validation to 'create' or 'update' operations\n"; |
||
98 | echo "\t\t\t),\n"; |
||
99 | endforeach; |
||
100 | echo "\t\t),\n"; |
||
101 | endforeach; |
||
102 | echo "\t);\n"; |
||
103 | endif; |
||
104 | |||
105 | foreach ($associations as $assoc): |
||
106 | if (!empty($assoc)): |
||
107 | ?> |
||
108 | |||
109 | // The Associations below have been created with all possible keys, those that are not needed can be removed |
||
110 | <?php |
||
111 | break; |
||
112 | endif; |
||
113 | endforeach; |
||
114 | |||
115 | foreach (array('hasOne', 'belongsTo') as $assocType): |
||
116 | if (!empty($associations[$assocType])): |
||
117 | $typeCount = count($associations[$assocType]); |
||
118 | echo "\n/**\n * $assocType associations\n *\n * @var array\n */"; |
||
119 | echo "\n\tpublic \$$assocType = array("; |
||
120 | foreach ($associations[$assocType] as $i => $relation): |
||
121 | $out = "\n\t\t'{$relation['alias']}' => array(\n"; |
||
122 | $out .= "\t\t\t'className' => '{$relation['className']}',\n"; |
||
123 | $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; |
||
124 | $out .= "\t\t\t'conditions' => '',\n"; |
||
125 | $out .= "\t\t\t'fields' => '',\n"; |
||
126 | $out .= "\t\t\t'order' => ''\n"; |
||
127 | $out .= "\t\t)"; |
||
128 | if ($i + 1 < $typeCount) { |
||
129 | $out .= ","; |
||
130 | } |
||
131 | echo $out; |
||
132 | endforeach; |
||
133 | echo "\n\t);\n"; |
||
134 | endif; |
||
135 | endforeach; |
||
136 | |||
137 | if (!empty($associations['hasMany'])): |
||
138 | $belongsToCount = count($associations['hasMany']); |
||
139 | echo "\n/**\n * hasMany associations\n *\n * @var array\n */"; |
||
140 | echo "\n\tpublic \$hasMany = array("; |
||
141 | foreach ($associations['hasMany'] as $i => $relation): |
||
142 | $out = "\n\t\t'{$relation['alias']}' => array(\n"; |
||
143 | $out .= "\t\t\t'className' => '{$relation['className']}',\n"; |
||
144 | $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; |
||
145 | $out .= "\t\t\t'dependent' => false,\n"; |
||
146 | $out .= "\t\t\t'conditions' => '',\n"; |
||
147 | $out .= "\t\t\t'fields' => '',\n"; |
||
148 | $out .= "\t\t\t'order' => '',\n"; |
||
149 | $out .= "\t\t\t'limit' => '',\n"; |
||
150 | $out .= "\t\t\t'offset' => '',\n"; |
||
151 | $out .= "\t\t\t'exclusive' => '',\n"; |
||
152 | $out .= "\t\t\t'finderQuery' => '',\n"; |
||
153 | $out .= "\t\t\t'counterQuery' => ''\n"; |
||
154 | $out .= "\t\t)"; |
||
155 | if ($i + 1 < $belongsToCount) { |
||
156 | $out .= ","; |
||
157 | } |
||
158 | echo $out; |
||
159 | endforeach; |
||
160 | echo "\n\t);\n\n"; |
||
161 | endif; |
||
162 | |||
163 | if (!empty($associations['hasAndBelongsToMany'])): |
||
164 | $habtmCount = count($associations['hasAndBelongsToMany']); |
||
165 | echo "\n/**\n * hasAndBelongsToMany associations\n *\n * @var array\n */"; |
||
166 | echo "\n\tpublic \$hasAndBelongsToMany = array("; |
||
167 | foreach ($associations['hasAndBelongsToMany'] as $i => $relation): |
||
168 | $out = "\n\t\t'{$relation['alias']}' => array(\n"; |
||
169 | $out .= "\t\t\t'className' => '{$relation['className']}',\n"; |
||
170 | $out .= "\t\t\t'joinTable' => '{$relation['joinTable']}',\n"; |
||
171 | $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n"; |
||
172 | $out .= "\t\t\t'associationForeignKey' => '{$relation['associationForeignKey']}',\n"; |
||
173 | $out .= "\t\t\t'unique' => 'keepExisting',\n"; |
||
174 | $out .= "\t\t\t'conditions' => '',\n"; |
||
175 | $out .= "\t\t\t'fields' => '',\n"; |
||
176 | $out .= "\t\t\t'order' => '',\n"; |
||
177 | $out .= "\t\t\t'limit' => '',\n"; |
||
178 | $out .= "\t\t\t'offset' => '',\n"; |
||
179 | $out .= "\t\t\t'finderQuery' => '',\n"; |
||
180 | $out .= "\t\t)"; |
||
181 | if ($i + 1 < $habtmCount) { |
||
182 | $out .= ","; |
||
183 | } |
||
184 | echo $out; |
||
185 | endforeach; |
||
186 | echo "\n\t);\n\n"; |
||
187 | endif; |
||
188 | ?> |
||
189 | } |