CakePHP 3 - 将数据保存为多个模型

时间:2015-11-03 19:57:30

标签: php validation cakephp models hierarchical

首先请原谅我的英语,不是我的母语。

这就是问题所在。我试图在cakephp 3中构造一个表单并将数据保存到两个模型中,saveAll()函数不起作用。

具体案例。

有用户,用户有几个数据,如用户名,密码,电子邮件等。 有医生,医生是有另一个领域的用户,"医生号码" 最后有患者,患者是有病的用户

关系是:

用户 - 医生 - > 0-1

医生 - 用户 - > 1-1

用户 - 患者 - > 0-1

患者 - 用户 - > 1-1

现在我的代码(多数民众代表烘焙生成的代码)(与医生的例子):

用户TableModel关系:

public function initialize(array $config)
    {
        parent::initialize($config);
        $this->table('users');
        $this->displayField('id');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');
        $this->belongsTo('Countries', [
            'foreignKey' => 'country_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Rols', [
            'foreignKey' => 'rol_id',
            'joinType' => 'INNER'
        ]);
        $this->hasOne('Doctors', [
            'foreignKey' => 'user_id'
        ]);
    }

用户验证:

 public function validationDefault(Validator $validator)
    {
        $validator
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('user_email', 'create')
            ->notEmpty('user_email')
            ->add('user_email', 'validFormat', [
                'rule' => 'email',
                'message' => 'E-mail must be valid'
            ]);

        $validator
            ->requirePresence('user_username', 'create')
            ->notEmpty('user_username');

        $validator
            ->requirePresence('user_pass', 'create')
            ->notEmpty('user_pass');

        $validator
            ->add('user_birthdate', 'valid', ['rule' => 'date'])
            ->requirePresence('user_birthdate', 'create')
            ->notEmpty('user_birthdate');

        $validator
            ->requirePresence('user_avatar', 'create')
            ->notEmpty('user_avatar');

        $validator
            ->requirePresence('user_dni', 'create')
            ->notEmpty('user_dni')
            ->add('user_dni', 'validFormat',[
                'rule' => array('custom', '((([X-Z]{1})([-]?))?(\d{7,8})([-]?)([A-Z]{1}))'),
                'message' => 'Please enter a valid DNI or NIE.'
            ]);

        $validator
            ->requirePresence('user_name', 'create')
            ->notEmpty('user_name');

        $validator
            ->requirePresence('user_lastname', 'create')
            ->notEmpty('user_lastname');

        $validator
            ->add('user_address', 'valid', ['rule' => 'numeric'])
            ->requirePresence('user_address', 'create')
            ->notEmpty('user_address');

        $validator
            ->add('user_locality', 'valid', ['rule' => 'numeric'])
            ->requirePresence('user_locality', 'create')
            ->notEmpty('user_locality');

        $validator
            ->requirePresence('user_postalcode', 'create')
            ->notEmpty('user_postalcode');

        $validator
            ->requirePresence('user_phone', 'create')
            ->notEmpty('user_phone');

        $validator
            ->add('last_conection', 'valid', ['rule' => 'datetime'])
            ->allowEmpty('last_conection');

        $validator
            ->add('active', 'valid', ['rule' => 'boolean'])
            ->allowEmpty('active');

        $validator
            ->allowEmpty('token');

        return $validator;
    }

Doctor TableModel关系:

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('doctors');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->hasOne('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
    }

医生视图添加:

<?= $this->Form->create($doctor) ?>
    <fieldset>
    <legend><?= __('Add Doctor') ?></legend>
        <?php
            echo $this->Form->input('User.user_email');
            echo $this->Form->input('User.user_username');
            echo $this->Form->input('User.user_pass');
            echo $this->Form->input('User.user_passconfirm');
            echo $this->Form->input('User.user_birthdate');
            echo $this->Form->input('User.user_avatar');
            echo $this->Form->input('User.user_dni');
            echo $this->Form->input('User.user_name');
            echo $this->Form->input('User.user_lastname');
            echo $this->Form->input('User.user_address');
            echo $this->Form->input('User.country_id', ['options' => $countries]);
            echo $this->Form->input('User.user_locality');
            echo $this->Form->input('User.user_postalcode');
            echo $this->Form->input('User.user_phone');
            echo $this->Form->input('User.rol_id', ['options' => $rols]);
            echo $this->Form->input('specialty');
        ?>
    </fieldset>

医生控制器添加方法:

public function add()
    {
        $doctor = $this->Doctors->newEntity();
        if ($this->request->is('post')) {
            $doctor = $this->Doctors->patchEntity($doctor, $this->request->data);
            if ($this->Doctors->save($doctor)) {
                $this->Flash->success(__('The doctor has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The doctor could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('doctor'));
        $this->set('_serialize', ['doctor']);
    }

症状:

  • 用户数据未验证
  • 用户数据未保存

照片

用户视图验证工作 View image

医生查看验证用户无法正常工作 View Image

感谢所有帮助我的人!

0 个答案:

没有答案