2012-09-03 69 views
0

我有如下僱員登記表,有一個與兩個實體關聯的表單。我需要讓一個實體可選 - symfony 2?

namespace Hrm\EmployeeBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use Hrm\UserBundle\Form\User\UserForm; 

class EmpForm extends AbstractType 
{ 
public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder->add('id','hidden'); 
    $builder->add('firs_name', 'text'); 
    $builder->add('middle_name', 'text'); 
    $builder->add('last_name', 'text');  
    $builder->add('user', new UserForm(), array('required' => false)); 
} 

public function getName() 
{ 
    return 'EmpForm'; 
} 

} 

也我有如下用戶登錄表單,

namespace Hrm\UserBundle\Form\User; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class UserForm extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
    $builder->add("id","hidden",array("required"=>false)); 
    $builder->add("userName","text",array("label"=>"Username")); 
    $builder->add("password","password",array("label"=>"Password")); 
    $builder->add("confirmPassword","password",array("property_path" => 
        false,"label"=>"Confirm Password")); 
    $builder->add("email","email",array("label"=>"Email")); 
    } 

    public function getName() 
    { 
    return 'user'; 
    } 
} 

僱員YML文件將作爲跟隨,

Hrm\EmployeeBundle\Entity\Employee: 
type: entity 
table: employee 
fields: 
    id: 
    type: integer 
    id: true 
    generator: 
     strategy: AUTO 
    firs_name: 
    type: string 
    length: '255' 
    default: null 
    middle_name: 
    type: string 
    length: '255' 
    default: null 
    last_name: 
    type: string 
    length: '255' 
    default: null 
    oneToOne: 
    User: 
     targetEntity: Hrm\UserBundle\Entity\User 
     cascade: ["remove", "persist"] 
     joinColumn: 
     name: login_id   
     referencedColumnName: id   
     nullable: true     
lifecycleCallbacks: { } 

員工控制器就是這樣,

public function addEmployeeAction(Request $request) { 

    $employee = new Employee(); 

    $user = new User(); 
    $employee->setUser($user); 

    $form = $this->createForm(new EmpForm(), $employee); 

    if ($request->getMethod() == 'POST') { 

     $form->bindRequest($request); 

     if ($form->isValid()) { 

      $em = $this->getDoctrine()->getEntityManager(); 
      $em->persist($employee); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('HrmEmployeeBundle_homepage')); 
     } 
    } 

    return $this->render('HrmEmployeeBundle:Pages:add.html.twig', array(
       'form' => $form->createView(), 'status' => 'addEmployee', 
      )); 
} 

所以我需要做的是,下列選項應該可用,

  1. 填寫員工登記詳細信息,並創建一個登錄(這已經完成)
  2. 只填寫員工登記信息,並不需要創建爲此登錄。

所以第二個選項不適合我,它說Column'username'不能爲空 我該如何擺脫這個問題?

+0

我不明白你的問題。這不是很清楚。 – Flukey

回答

0

如果你想設置一個SQL場optionnal,添加可空期權屬實:

oneToOne: 
    User: 
     targetEntity: Sys\UserBundle\Entity\User 
     cascade: ["persist"] 
     joinColumn: 
     name: login_id 
     referencedColumnName: id 
     nullable: true 

但在你的情況下,它似乎是您註冊一個沒有用戶名,這是不符合邏輯的用戶...也許用戶名字段工作不正常,請給我們更多的細節。

+0

感謝您的回覆Sybio。我再次編輯了這個問題。請幫助我,再次感謝。 – Rohitha

1

要回答你的第二個問題,在您的addEmployeeAction,不要對新員工添加用戶,刪除這些行:

$user = new User(); 
$employee->setUser($user); 

目前,$員工保存一個空的用戶,誰不沒有用戶名,所以MySQL不高興! 通過刪除這些行,用戶將被創建,只有當它的形式被填充!

+0

我按照上面的說法試過了。但仍然有以下錯誤。可捕獲的致命錯誤:傳遞給Hrm \ EmployeeBundle \ Entity \ Employee :: setUser()的參數1必須是Hrm \ UserBundle \ Entity \ User的一個實例,在給定的數組中給出,我在這裏找到了類似的問題http:// stackoverflow的.com /問題/ 9041671/symfony2-1到1-0-關係功能於一個嵌入形式與 - 教義 – Rohitha

相關問題