【问题标题】:symfony/doctrine getOrganisation() on employee returns a string (but I want an object)symfony/doctrine getOrganisation() on employee 返回一个字符串(但我想要一个对象)
【发布时间】:2011-11-15 08:44:15
【问题描述】:

我正在使用 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 类的数据库。

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

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

【问题讨论】:

    标签: php orm symfony1 doctrine


    【解决方案1】:

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

    最好遵循 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() 获取ID,并使用$emplyee->Organisation$employee->getOrganisation() 之类的组织。

    【讨论】:

    • 不要被 Grad 将“组织”重新拼写为“组织”(北美以外首选的拼写)误导 - 这无关紧要。
    • @Colin Fine:哈哈,好吧,但我不在北美,我更喜欢 z。 :P
    • 这两种拼写在英国都可以接受,一些权威机构(例如牛津大学出版社)更喜欢“ize”;但大多数人使用'ise'。在美国,一般来说,只使用“ize”,以至于许多美国人看到“ise”就认为这是一个错误并想要纠正它。
    • @van Horck:我只是尝试编辑并更正您答案中的错字($emplyee 应该是 $employee)。但它告诉我,编辑必须至少有 6 个字符(不确定我是否知道这意味着什么)。也许你可以纠正它?
    猜你喜欢
    • 2013-11-12
    • 2018-12-21
    • 2020-11-02
    • 2018-08-12
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多