2015-12-21 101 views
0

我有兩個表員工和用戶員工之間有字段user_id關係我們用戶有一個員工和員工屬於用戶,我在我的視圖文件中有以下代碼cakephp 3.x模型驗證沒有顯示何時將數據保存到2個表

<?php 
    echo $this->Form->input('employee.name'); 
    echo $this->Form->input('user.email'); 
    echo $this->Form->input('user.username'); 
    echo $this->Form->input('employee.employee_level'); 
?> 

我EmployeeController代碼

<?php 

    $employee = $this->Employees->newEntity($this->request->data); 
    $user = $this->Employees->Users->newEntity($this->request->data); 
     if ($this->request->is('post')) { 

      $employee = $this->Employees->patchEntity($employee, $this->request->data, [ 
       'associated' => ['Users'] 
      ]); 

      if($this->Employees->save($employee)) { 

       $this->Flash->success(__('The employee has been saved.')); 

      } else { 
       $this->Flash->error(__('The employee could not be saved. Please, try again.')); 
      } 
     } 
?> 

其節能,如果所有的事情會好和完美的,但如果有喜歡的用戶名某些驗證錯誤已經存在或電子郵件已經比它的簡單表演閃光燈消息存在數據該員工可以OT被保存,請重試它沒有顯示下的字段模型驗證錯誤消息,如圖所示,附加的圖像時,我嘗試將數據保存在單個表 enter image description here

請告訴我什麼是我的視圖或控制器問題文件,以便任何兩個模型具有驗證錯誤比相關領域

對不起根據其顯示錯誤消息,我的英文不好 感謝

回答

1

嘗試此表單。您不需要提供頂級模型的名稱。

echo $this->Form->input('name'); 
echo $this->Form->input('user.email'); 
echo $this->Form->input('user.username'); 
echo $this->Form->input('employee_level'); 

而在你的控制器,你永遠不使用$用戶,在創建它所以沒有點,因爲你修補與請求數據的僱員實體中,不需要初始化同樣的方式。

$employee = $this->Employees->newEntity(); 
if ($this->request->is('post')) { 
    $employee = $this->Employees->patchEntity($employee, $this->request->data, [ 
     'associated' => ['Users'] 
    ]); 
... 
+0

謝謝你這麼多的解決我的問題,你是真棒只是提到這個答案批准答案我有一個問題,如果你能幫助我什麼是鑑於語法有一個像<?PHP的回聲許多類型的字段$ this-> Form-> input('image.0.image');?>,<?php echo $ this-> Form-> input('image.1.image');?>什麼是控制器代碼爲此 –

+0

這在[手冊](http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data)中有詳細的介紹。 –

+0

非常感謝我的朋友,你能否告訴我如何打印關聯數據值的詳細信息與paginate類似員工表有用戶ID和用戶有role_id現在當我使用代碼$ this-> paginate($ this-> Employees)比我想獲得與用戶表有關的角色名稱,當我打印他查詢並運行在phpmyadmin其顯示詳細信息,但是當我使用用戶$ employee-> has('user')的代碼? $ employee-> user-> email:「」比打印來自用戶的電子郵件,但是如何將角色表simillar打印到此 謝謝 –

相關問題