teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

補足追記

2015/12/15 07:39

投稿

otagroove
otagroove

スコア28

title CHANGED
File without changes
body CHANGED
@@ -9,4 +9,120 @@
9
9
  サイト:http://y-stream.blogspot.jp/2013/07/cakephp-eclipse-codehint.html
10
10
 
11
11
  お手数おかけいたしますが、ご教示いただければ幸いです。
12
- よろしくお願い致します。
12
+ よろしくお願い致します。
13
+
14
+
15
+ ###補足
16
+
17
+ **1.開発環境**
18
+ - eclipse4.5.1
19
+ - CakePHP3.1.5
20
+
21
+ **2.モデル情報**
22
+
23
+ Modelは自動生成したモデル
24
+ 使用コマンド:bin\cake bake model persons
25
+ なお、生成されてソースは下記に添付いたします。
26
+
27
+ 今回コードを補完したいのはPersonsTableクラスに入っている「newEntity」になります。
28
+ なお、コントローラークラスでは下記のように呼び出しております。
29
+ ```PHP
30
+ class PersonsController extends AppController {
31
+ public function add() {
32
+ $person = $this->Persons->newEntity ();
33
+ }
34
+ }
35
+ ```
36
+
37
+ このCakePHPでインスタンス化されたPersonsTableクラスの「newEntity」というメソッドのコードを保管したいです。
38
+
39
+ お手数おかけいたしますが、何卒よろしくお願い致します。
40
+
41
+ ・生成されたテーブルクラス
42
+ ```PHP
43
+ <?php
44
+
45
+ namespace App\Model\Table;
46
+
47
+ use App\Model\Entity\Person;
48
+ use Cake\ORM\Query;
49
+ use Cake\ORM\RulesChecker;
50
+ use Cake\ORM\Table;
51
+ use Cake\Validation\Validator;
52
+
53
+ /**
54
+ * Persons Model
55
+ */
56
+ class PersonsTable extends Table {
57
+
58
+ /**
59
+ * Initialize method
60
+ *
61
+ * @param array $config
62
+ * The configuration for the Table.
63
+ * @return void
64
+ */
65
+ public function initialize(array $config) {
66
+ parent::initialize ($config);
67
+
68
+ $this->table ('persons');
69
+ $this->displayField ('name');
70
+ $this->primaryKey ('id');
71
+ }
72
+
73
+ /**
74
+ * Default validation rules.
75
+ *
76
+ * @param \Cake\Validation\Validator $validator
77
+ * Validator instance.
78
+ * @return \Cake\Validation\Validator
79
+ */
80
+ public function validationDefault(Validator $validator) {
81
+ $validator->add ('id', 'valid', [
82
+ 'rule' => 'numeric' ])->allowEmpty ('id', 'create');
83
+
84
+ $validator->requirePresence ('name', 'create')->notEmpty ('name');
85
+
86
+ $validator->add ('age', 'valid', [
87
+ 'rule' => 'numeric' ])->allowEmpty ('age');
88
+
89
+ $validator->allowEmpty ('mail');
90
+
91
+ return $validator;
92
+ }
93
+ }
94
+ ```
95
+
96
+ ・生成されたEntitiyクラス
97
+ ```PHP
98
+ <?php
99
+
100
+ namespace App\Model\Entity;
101
+
102
+ use Cake\ORM\Entity;
103
+
104
+ /**
105
+ * Person Entity.
106
+ *
107
+ * @property int $id
108
+ * @property string $name
109
+ * @property int $age
110
+ * @property string $mail
111
+ */
112
+ class Person extends Entity {
113
+
114
+ /**
115
+ * Fields that can be mass assigned using newEntity() or patchEntity().
116
+ *
117
+ * Note that when '*' is set to true, this allows all unspecified fields to
118
+ * be mass assigned. For security purposes, it is advised to set '*' to false
119
+ * (or remove it), and explicitly make individual fields accessible as needed.
120
+ *
121
+ * @var array
122
+ */
123
+ protected $_accessible = [
124
+ '*' => true,
125
+ 'id' => false ];
126
+ }
127
+
128
+ ```