table.ctp
5.3 KB
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
<%
/**
* 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 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Utility\Inflector;
%>
<?php
namespace <%= $namespace %>\Model\Table;
<%
$uses = [
"use $namespace\\Model\\Entity\\$entity;",
'use Cake\ORM\Query;',
'use Cake\ORM\RulesChecker;',
'use Cake\ORM\Table;',
'use Cake\Validation\Validator;'
];
sort($uses);
echo implode("\n", $uses);
%>
/**
* <%= $name %> Model
<% if ($associations): %>
*
<% foreach ($associations as $type => $assocs): %>
<% foreach ($assocs as $assoc): %>
* @property \Cake\ORM\Association\<%= Inflector::camelize($type) %> $<%= $assoc['alias'] %>
<% endforeach %>
<% endforeach; %>
<% endif; %>
*/
class <%= $name %>Table extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
<% if (!empty($table)): %>
$this->table('<%= $table %>');
<% endif %>
<% if (!empty($displayField)): %>
$this->displayField('<%= $displayField %>');
<% endif %>
<% if (!empty($primaryKey)): %>
<% if (count($primaryKey) > 1): %>
$this->primaryKey([<%= $this->Bake->stringifyList((array)$primaryKey, ['indent' => false]) %>]);
<% else: %>
$this->primaryKey('<%= current((array)$primaryKey) %>');
<% endif %>
<% endif %>
<% if (!empty($behaviors)): %>
<% endif; %>
<% foreach ($behaviors as $behavior => $behaviorData): %>
$this->addBehavior('<%= $behavior %>'<%= $behaviorData ? ", [" . implode(', ', $behaviorData) . ']' : '' %>);
<% endforeach %>
<% if (!empty($associations['belongsTo']) || !empty($associations['hasMany']) || !empty($associations['belongsToMany'])): %>
<% endif; %>
<% foreach ($associations as $type => $assocs): %>
<% foreach ($assocs as $assoc):
$alias = $assoc['alias'];
unset($assoc['alias']);
%>
$this-><%= $type %>('<%= $alias %>', [<%= $this->Bake->stringifyList($assoc, ['indent' => 3]) %>]);
<% endforeach %>
<% endforeach %>
}
<% if (!empty($validation)): %>
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
<%
foreach ($validation as $field => $rules):
$validationMethods = [];
foreach ($rules as $ruleName => $rule):
if ($rule['rule'] && !isset($rule['provider'])):
$validationMethods[] = sprintf("->%s('%s')", $rule['rule'], $field);
elseif ($rule['rule'] && isset($rule['provider'])):
$validationMethods[] = sprintf(
"->add('%s', '%s', ['rule' => '%s', 'provider' => '%s'])",
$field,
$ruleName,
$rule['rule'],
$rule['provider']
);
endif;
if (isset($rule['allowEmpty'])):
if (is_string($rule['allowEmpty'])):
$validationMethods[] = sprintf(
"->allowEmpty('%s', '%s')",
$field,
$rule['allowEmpty']
);
elseif ($rule['allowEmpty']):
$validationMethods[] = sprintf(
"->allowEmpty('%s')",
$field
);
else:
$validationMethods[] = sprintf(
"->requirePresence('%s', 'create')",
$field
);
$validationMethods[] = sprintf(
"->notEmpty('%s')",
$field
);
endif;
endif;
endforeach;
if (!empty($validationMethods)):
$lastIndex = count($validationMethods) - 1;
$validationMethods[$lastIndex] .= ';';
%>
$validator
<%- foreach ($validationMethods as $validationMethod): %>
<%= $validationMethod %>
<%- endforeach; %>
<%
endif;
endforeach;
%>
return $validator;
}
<% endif %>
<% if (!empty($rulesChecker)): %>
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
<%- foreach ($rulesChecker as $field => $rule): %>
$rules->add($rules-><%= $rule['name'] %>(['<%= $field %>']<%= !empty($rule['extra']) ? ", '$rule[extra]'" : '' %>));
<%- endforeach; %>
return $rules;
}
<% endif; %>
<% if ($connection !== 'default'): %>
/**
* Returns the database connection name to use by default.
*
* @return string
*/
public static function defaultConnectionName()
{
return '<%= $connection %>';
}
<% endif; %>
}