データの保存ができなくて困っています。
debugするとフォームからデータは取得できているのですが、saveされず保存できませんでしたのflashが表示されます。
mysqlを見ても新規登録はされていません。
ちなみに登録後の編集はできており、users意外のテーブルやcontrollerなどは正常に動いています。
UsersTable.php(table)
<?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; class UsersTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); $this->setDisplayField('username'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); //部署テーブルと内部結合 $this->belongsTo('Departments', [ 'foreignKey' => 'department_id', 'joinType' => 'INNER' ]); } public function validationDefault(Validator $validator) { $validator ->integer('id') ->allowEmpty('id', 'create'); $validator ->scalar('username') ->maxLength('username', 50) ->requirePresence('username', 'create') ->notEmpty('username'); $validator ->scalar('password') ->maxLength('password', 255) ->requirePresence('password', 'create') ->notEmpty('password'); $validator ->scalar('role') ->maxLength('role', 20) ->requirePresence('role', 'create') ->notEmpty('role'); $validator ->integer('enabled') ->requirePresence('enabled', 'create') ->notEmpty('enabled'); $validator ->scalar('name_last') ->maxLength('name_last', 50) ->requirePresence('name_last', 'create') ->notEmpty('name_last'); $validator ->scalar('name_first') ->maxLength('name_first', 50) ->requirePresence('name_first', 'create') ->notEmpty('name_first'); $validator ->scalar('oldpassword') ->maxLength('oldpassword', 255) ->requirePresence('oldpassword', 'create') ->notEmpty('oldpassword'); $validator ->scalar('newpassword') ->maxLength('newpassword', 255) ->requirePresence('newpassword', 'create') ->notEmpty('newpassword'); return $validator; } public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['username'])); return $rules; } }
User.php(entity)
<?php namespace App\Model\Entity; use Cake\ORM\Entity; use Cake\Auth\DefaultPasswordHasher; class User extends Entity { protected $_accessible = [ 'username' => true, 'password' => true, 'role' => true, 'enabled' => true, 'name_last' => true, 'name_first' => true, 'created' => true, 'modified' => true, 'department_id' => true, 'department'=>true ]; protected $_hidden = [ 'password' ]; protected function _setPassword($password){ return (new DefaultPasswordHasher)->hash($password); } }
UsersController.php(controller)
<?php namespace App\Controller; use App\Controller\AppController; use Cake\Auth\DefaultPasswordHasher; //added. use Cake\Event\Event; //added. use Cake\Log\Log; use Cake\ORM\TableRegistry; class UsersController extends AppController { private $departments; public $paginate=[ 'contain'=>'Departments', 'limit'=>10, 'sort'=>'id', 'direction'=>'asc' ]; public function initialize(){ parent::initialize(); $this->departments=TableRegistry::get('Departments'); } public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { $user = $this->Users->patchEntity($user,$this->request->getData()); $data=$this->request->getData(); Log::debug("user.add"); Log::debug($data); Log::debug($data['username']); Log::debug($data['password']); Log::debug($data['role']); Log::debug($data['enabled']); Log::debug($data['name_last']); Log::debug($data['name_first']); Log::debug($data['department_id']); if ($this->Users->save($user)) { $this->Flash->success(__('保存しました。')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('保存できませんでした。再度実行してください。')); } $this->set(compact('user')); $this->set('departments',$this->departments->find('list',[ 'keyField'=>'id', 'valueField'=>'department' ])); } }
add.ctp(view)
<?php ?> <?= $this->Form->create($user) ?> <fieldset> <legend> <?= __('新規登録') ?> </legend> <?php echo $this->Form->control('username'); echo $this->Form->control('password'); echo $this->Form->select('role',['admin'=>'admin', 'reader'=>'reader', 'user'=>'user']); echo $this->Form->control('enabled'); echo $this->Form->control('name_last'); echo $this->Form->control('name_first'); echo $this->Form->input('department_id'); ?> </fieldset> <?= $this->Form->button(__('新規登録')) ?> <?= $this->Form->end() ?> </div>
debug::log
2019-02-03 00:17:59 Debug: user.add 2019-02-03 00:17:59 Debug: Array ( [username] => aaa [password] => aaa [role] => user [enabled] => 1 [name_last] => aaa [name_first] => aaa [department_id] => 1 ) 2019-02-03 00:17:59 Debug: aaa 2019-02-03 00:17:59 Debug: aaa 2019-02-03 00:17:59 Debug: user 2019-02-03 00:17:59 Debug: 1 2019-02-03 00:17:59 Debug: aaa 2019-02-03 00:17:59 Debug: aaa 2019-02-03 00:17:59 Debug: 1
回答1件
あなたの回答
tips
プレビュー