|
| 1 | +<% |
| 2 | +use Cake\Utility\Inflector; |
| 3 | +%> |
| 4 | +<?php |
| 5 | +namespace <%= $namespace %>\Model\Table; |
| 6 | + |
| 7 | +<% |
| 8 | +$uses = [ |
| 9 | + "use $namespace\\Model\\Entity\\$entity;", |
| 10 | + 'use Cake\ORM\Query;', |
| 11 | + 'use Cake\ORM\RulesChecker;', |
| 12 | + 'use Cake\ORM\Table;', |
| 13 | + 'use Cake\Validation\Validator;' |
| 14 | +]; |
| 15 | +sort($uses); |
| 16 | +echo implode("\n", $uses); |
| 17 | +%> |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * <%= $name %> Model |
| 22 | +<% if ($associations): %> |
| 23 | + * |
| 24 | +<% foreach ($associations as $type => $assocs): %> |
| 25 | +<% foreach ($assocs as $assoc): %> |
| 26 | + * @property \Cake\ORM\Association\<%= Inflector::camelize($type) %> $<%= $assoc['alias'] %> |
| 27 | +<% endforeach %> |
| 28 | +<% endforeach; %> |
| 29 | +<% endif; %> |
| 30 | + */ |
| 31 | +class <%= $name %>Table extends Table |
| 32 | +{ |
| 33 | + |
| 34 | + /** |
| 35 | + * Initialize method |
| 36 | + * |
| 37 | + * @param array $config The configuration for the Table. |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function initialize(array $config) |
| 41 | + { |
| 42 | +<% if (!empty($table)): %> |
| 43 | + $this->table('<%= $table %>'); |
| 44 | +<% endif %> |
| 45 | +<% if (!empty($displayField)): %> |
| 46 | + $this->displayField('<%= $displayField %>'); |
| 47 | +<% endif %> |
| 48 | +<% if (!empty($primaryKey)): %> |
| 49 | +<% if (count($primaryKey) > 1): %> |
| 50 | + $this->primaryKey([<%= $this->Bake->stringifyList((array)$primaryKey, ['indent' => false]) %>]); |
| 51 | +<% else: %> |
| 52 | + $this->primaryKey('<%= current((array)$primaryKey) %>'); |
| 53 | +<% endif %> |
| 54 | +<% endif %> |
| 55 | + |
| 56 | +<% foreach ($behaviors as $behavior => $behaviorData): %> |
| 57 | + $this->addBehavior('<%= $behavior %>'<%= $behaviorData ? ", [" . implode(', ', $behaviorData) . ']' : '' %>); |
| 58 | +<% endforeach %> |
| 59 | + |
| 60 | +<% foreach ($associations as $type => $assocs): %> |
| 61 | +<% foreach ($assocs as $assoc): |
| 62 | + $alias = $assoc['alias']; |
| 63 | + unset($assoc['alias']); |
| 64 | +%> |
| 65 | + $this-><%= $type %>('<%= $alias %>', [<%= $this->Bake->stringifyList($assoc, ['indent' => 3]) %>]); |
| 66 | +<% endforeach %> |
| 67 | +<% endforeach %> |
| 68 | + } |
| 69 | +<% if (!empty($validation)): %> |
| 70 | + |
| 71 | + /** |
| 72 | + * Default validation rules. |
| 73 | + * |
| 74 | + * @param \Cake\Validation\Validator $validator Validator instance. |
| 75 | + * @return \Cake\Validation\Validator |
| 76 | + */ |
| 77 | + public function validationDefault(Validator $validator) |
| 78 | + { |
| 79 | + $validator |
| 80 | +<% $validationMethods = []; %> |
| 81 | +<% |
| 82 | +$firstField = true; |
| 83 | +foreach ($validation as $field => $rules): |
| 84 | + if ($firstField !== true): |
| 85 | + $validationMethods[] = "\n \$validator"; |
| 86 | + endif; |
| 87 | + |
| 88 | + foreach ($rules as $ruleName => $rule): |
| 89 | + if ($rule['rule'] && !isset($rule['provider'])): |
| 90 | + $validationMethods[] = sprintf( |
| 91 | + "->add('%s', '%s', ['rule' => '%s'])", |
| 92 | + $field, |
| 93 | + $ruleName, |
| 94 | + $rule['rule'] |
| 95 | + ); |
| 96 | + elseif ($rule['rule'] && isset($rule['provider'])): |
| 97 | + $validationMethods[] = sprintf( |
| 98 | + "->add('%s', '%s', ['rule' => '%s', 'provider' => '%s'])", |
| 99 | + $field, |
| 100 | + $ruleName, |
| 101 | + $rule['rule'], |
| 102 | + $rule['provider'] |
| 103 | + ); |
| 104 | + endif; |
| 105 | + |
| 106 | + if (isset($rule['allowEmpty'])): |
| 107 | + if (is_string($rule['allowEmpty'])): |
| 108 | + $validationMethods[] = sprintf( |
| 109 | + "->allowEmpty('%s', '%s')", |
| 110 | + $field, |
| 111 | + $rule['allowEmpty'] |
| 112 | + ); |
| 113 | + elseif ($rule['allowEmpty']): |
| 114 | + $validationMethods[] = sprintf( |
| 115 | + "->allowEmpty('%s')", |
| 116 | + $field |
| 117 | + ); |
| 118 | + else: |
| 119 | + $validationMethods[] = sprintf( |
| 120 | + "->requirePresence('%s', 'create')", |
| 121 | + $field |
| 122 | + ); |
| 123 | + $validationMethods[] = sprintf( |
| 124 | + "->notEmpty('%s')", |
| 125 | + $field |
| 126 | + ); |
| 127 | + endif; |
| 128 | + endif; |
| 129 | + endforeach; |
| 130 | + $firstField = false; |
| 131 | + $validationMethods[] = array_pop($validationMethods) . ";"; |
| 132 | +endforeach; |
| 133 | +%> |
| 134 | +<%= " " . implode("\n ", $validationMethods) %> |
| 135 | + |
| 136 | + |
| 137 | + return $validator; |
| 138 | + } |
| 139 | +<% endif %> |
| 140 | +<% if (!empty($rulesChecker)): %> |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns a rules checker object that will be used for validating |
| 144 | + * application integrity. |
| 145 | + * |
| 146 | + * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. |
| 147 | + * @return \Cake\ORM\RulesChecker |
| 148 | + */ |
| 149 | + public function buildRules(RulesChecker $rules) |
| 150 | + { |
| 151 | + <%- foreach ($rulesChecker as $field => $rule): %> |
| 152 | + $rules->add($rules-><%= $rule['name'] %>(['<%= $field %>']<%= !empty($rule['extra']) ? ", '$rule[extra]'" : '' %>)); |
| 153 | + <%- endforeach; %> |
| 154 | + return $rules; |
| 155 | + } |
| 156 | +<% endif; %> |
| 157 | +<% if ($connection != 'default'): %> |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns the database connection name to use by default. |
| 161 | + * |
| 162 | + * @return string |
| 163 | + */ |
| 164 | + public static function defaultConnectionName() |
| 165 | + { |
| 166 | + return '<%= $connection %>'; |
| 167 | + } |
| 168 | +<% endif; %> |
| 169 | +} |
0 commit comments