cakephp 3.* 保存 belongsTo 数据

cakephp 3.* save belongsTo data

我有一个前端公式来添加新用户。此外,它应该将用户公司添加到公司 table 并将新用户的 company_id 设置为新公司的 ID。提交时出现错误 'Unable to add the user.'

(它是一个用户属于公司协会)

在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->data returns:

    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;
}

使用这个debug($user->errors());查看验证是否出错。这将打印出每列的数组验证错误(如果有)。