员工上的symfony / doctrine getOrganisation()返回一个字符串(但我想要一个对象)

时间:2011-09-12 13:29:59

标签: php orm symfony1 doctrine

我正在使用symfony 1.4&教义。 1.2

在我的schema.yml中,我定义了一个员工和一个组织实体:

employee:
  tableName: employee
  columns:
    id:
      primary: true
      type: integer(8)
      notnull: true
      autoincrement: true
    organization:
      default: NULL
      type: integer
  relations:
    organization:
      onDelete: restrict
      local: organization
      foreign: id
organization:
  tableName: organization
  columns:
    id:
      primary: true
      unique: true
      type: integer
      notnull: true
      autoincrement: true
  relations:
    employee:
      type: many
      class: employee
      local: id
      foreign: organization

然后我运行命令symfony doctrine:build --all --and-load,这(重新)根据schema.yml创建包含表和php-classes的数据库。

所以当我现在做$employee->getOrganization()(假设$ employee是班级员工)时,我希望得到一个班级组织的对象。但是我得到了一个包含组织id字段内容的字符串。 当我以相反的方式尝试它时:$organization->getEmployee()(假设$ organization是类组织)它会向所有员工返回Doctrine_Collection。

我如何getOrganization()返回组织对象?

1 个答案:

答案 0 :(得分:2)

它不起作用,因为您的本地字段和关系具有相同的名称(“组织”)。

最好遵循Doctrine命名'指南':

employee:
  tableName: employee
  columns:
    id:
      primary: true
      type: integer(8)
      notnull: true
      autoincrement: true
    organisation_id:   # renamed to 'organisation_id'
      default: NULL
      type: integer
  relations:
    Organisation:      # capitalized
      onDelete: restrict
      local: organisation_id #renamed to 'organisation_id'
      foreign: id

现在,您可以使用$employee->organisation_id$employee->getOrganisationId()以及$emplyee->Organisation$employee->getOrganisation()等组织获取ID。