0

我正在尝试合并两个实体(用户和学生)来创建一个表单,下面是我的 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 中合并两个表单?

任何帮助都感激不尽...

4

1 回答 1

0

这是因为 CRUD 生成器不支持自定义的 id,例如 student_id 等...参见代码。如下所示,如果 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;
}
于 2012-08-09T02:39:33.753 回答