有一个与两个实体相关联的表格。我需要让一个实体可选 - symfony 2?

时间:2012-09-03 06:12:16

标签: php symfony doctrine-orm

我有一份员工登记表,如下所示,

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. 仅填写员工注册详细信息,无需为此创建登录信息。
  3. 所以第二个选项不适合我,它说Column'username'不能为null 我该怎样摆脱这个问题?

2 个答案:

答案 0 :(得分:1)

要回答第二个问题,请在addEmployeeAction中添加新员工的用户,删除以下行:

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

目前,$ employee使用空用户保存,没有用户名,因此MySQL不满意! 通过删除这些行,只有在填写表单时才会创建用户!

答案 1 :(得分:0)

如果要设置SQL字段optionnal,请添加nullable选项true:

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

但是在你的情况下,它似乎你注册了一个没有用户名的用户,这是不合逻辑的...也许用户名字段工作不正常,请给我们更多细节。