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