Php employee上的symfony/getOrganization()返回一个字符串(但我需要一个对象)
我正在与symfony 1.4和doctrine合作一个项目。1.2 在my schema.yml中,我定义了员工和组织实体:Php employee上的symfony/getOrganization()返回一个字符串(但我需要一个对象),php,orm,symfony1,doctrine,Php,Orm,Symfony1,Doctrine,我正在与symfony 1.4和doctrine合作一个项目。1.2 在my schema.yml中,我定义了员工和组织实体: employee: tableName: employee columns: id: primary: true type: integer(8) notnull: true autoincrement: true organization: default: NULL ty
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属于类employee)时,我希望得到类organization的对象。但我得到一个字符串,其中包含组织id字段的内容。
当我以另一种方式尝试时:$organization->getEmployee()
(假设$organization属于类organization),它返回一个包含所有员工的原则集合
如何
getOrganization()
返回组织对象?它不起作用,因为您的本地字段和关系都具有相同的名称(“组织”)
最好遵循命名为“指南”的原则:
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->organization\u ID
或$employee->getorganizationid()
获取ID,而像$employee->organization>或$employee->getorganization()
这样的组织,不要被毕业生将“organization”重新称为“organization”(北美以外的首选拼写)所误导-那没关系。@Colin Fine:哈哈,好吧,但我在NA之外,更喜欢z:PBoth拼写在英国是可以接受的,一些权威机构(如牛津大学出版社)更喜欢“ize”;但大多数人使用“ise”。在美国,通常只使用“ize”,以至于很多美国人如果看到“ise”,就会认为这是一个错误,并想纠正它。@van Horck:我只是试着编辑和纠正你答案中的一个拼写错误($employee应该是$employee)。但它告诉我,编辑必须至少是6个字符(不确定我是否知道这意味着什么)。也许你能纠正它?