cakephp 3. *保存belongsTo数据

时间:2016-01-13 15:45:03

标签: php cakephp-3.0 saving-data

我有一个前端公式来添加新用户。此外,它应将Users Company添加到Companies表,并将新User的company_id设置为新的Companys id。提交时,我收到错误消息“无法添加用户。”#/ p>

(用户所属的公司协会)

在UsersController.php中:

public function add()
{
    $this->viewBuilder()->layout('login');
    $user = $this->Users->newEntity($this->request->data,['associated' => ['Companies']]);
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user,['associated' => ['Companies']]);
        if ($this->Users->save($user) ) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'login']);
        }
        $this->Flash->error(__('Unable to add the user.'));
    }
    $this->set('user', $user);

    $this->loadModel('Companies');
    $this->set('companies', $this->Companies->find('all',array('fields'=>array('name','id'))));
}

$ this-> request->数据的调试返回:

    Array
(
   [username] => the name
   [email] => dat@goas.uups
   [password] => test
   [company] => Array
       (
           [name] => comanysnametest
           [iban] => DE02 520934 5  02384
           [bic] => Genoht 034
           [kontoinhaber] => the name here
           [straße] => alphastr
           [hausnummer] => 1337
           [plz] => 46467
           [ort] => Supp
           [geschäftsführung] => another name 
           [vat] => 038u pwdf8a spdf
       )

)

修改

UsersTable :: validationDefault:

public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->add('email', 'valid', ['rule' => 'email'])
        ->requirePresence('email', 'create')
        ->notEmpty('email')
        ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

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

    $validator
        ->requirePresence('username', 'create')
        ->notEmpty('username')
        ->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

    $validator
        ->allowEmpty('role');

    return $validator;
}

CompaniesTable :: validationDefault

public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->requirePresence('name', 'create')
        ->notEmpty('name')
        ->add('name', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

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

    $validator
        ->allowEmpty('iban');

    $validator
        ->allowEmpty('bic');

    $validator
        ->allowEmpty('kontoinhaber');

    $validator
        ->requirePresence('straße', 'create')
        ->notEmpty('straße');

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

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

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

    $validator
        ->requirePresence('geschäftsführung', 'create')
        ->notEmpty('geschäftsführung');

    $validator
        ->allowEmpty('vat');

    return $validator;
}

1 个答案:

答案 0 :(得分:1)

使用此调试($ user-> errors());验证验证是否出错。如果有的话,这将打印出每列的数组验证错误。