2016-01-13 343 views
0

我有一個前端形式來添加一個新的用戶。另外,它應該將Users Company添加到Companies表中,並將新用戶的company_id設置爲新的Companys ID。提交時,我收到一條錯誤「無法添加用戶」。cakephp 3. *保存belongsTo數據

(其用戶屬於關聯公司協會)

在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')))); 
} 

的$這 - 調試>請求 - >數據返回:

Array 
(
    [username] => the name 
    [email] => [email protected] 
    [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; 
} 
+0

請發佈您的'UsersTable :: validationDefault()'。 「公司」一樣。 –

+0

調試'$ this-> Users-> save()'的返回值... – ndm

+0

$ this-> Users-> save()的調試只返回false – Ramses

回答

1

使用此調試($用戶>錯誤());看看驗證是否出錯。這將打印出每列的數組驗證錯誤(如果有的話)。

+0

謝謝!有「模塊」字段不應在表單中表示。現在它被設置爲默認值。所以一切正常 – Ramses