在Symfony 2中尝试合并两个表单(实体)时出错

时间:2012-08-09 02:33:28

标签: symfony doctrine doctrine-orm

我正在尝试合并两个实体(用户和学生)来创建单个表单,下面是我的orm文件

Ens\JobeetBundle\Entity\User:
  type: entity
  table: abc_user
  id:
    user_id:       { type: integer, generator: { strategy: AUTO } }
  fields:

    username: { type: string, length: 255, notnull: true, unique: true }
    email:    { type: string, length: 255, notnull: true, unique: true }
    password: { type: string, length: 255, notnull: true }
    enabled:  { type: boolean }
  oneToOne:
    student:
      targetEntity: Ens\JobeetBundle\Entity\Student
      mappedBy: user

Ens\JobeetBundle\Entity\Student:
  type: entity
  table: abc_student
  id:
    student_id: { type: integer, generator: { strategy: AUTO } }        
  fields:
    first_name: { type: string, length: 255, notnull: true }
    middle_name: { type: string, length: 255 }
    last_name: { type: string, length: 255, notnull: true }
  oneToOne:
    user:
      targetEntity: Ens\JobeetBundle\Entity\User
      joinColumn:
        name: user_id
        referencedColumnName: user_id

创建实体和更新方案工作正常,

php app/console doctrine:generate:entities EnsJobeetBundle

php app/console doctrine:database:update --force

但是在尝试生成crud时

php app/console generate:doctrine:crud --entity=EnsJobeetBundle:Student

我最终遇到以下错误,

[RuntimeException]

The CRUD generator expects the entity object has a primary key field named "id" with a getId() method.

有没有人知道如何摆脱这个?如何在Symfony 2中合并两个表单?

非常感谢任何帮助...

1 个答案:

答案 0 :(得分:0)

这是因为CRUD生成器不支持自定义的id,例如student_id等... 见code。如下所示,如果id不在您的实体中,您将获得运行时异常。

//....
if (!in_array('id', $metadata->identifier)) 
{
    throw new \RuntimeException('The CRUD generator expects the entity object has a primary key field named "id" with a getId() method.');
}
//....

您必须在模型中重命名自定义ID:

用户:

protected $id;

public function getId()
{
    return $this->id;
}

<强>学生:

protected $id;

public function getId()
{
    return $this->id;
}