2011-09-12 24 views
0

我正在使用symfony 1.4 &原理開展一個項目。 1.2員工上的symfony/doctrine getOrganisation()返回一個字符串(但我想要一個對象)

以我的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,此(重新)創建具有根據表和PHP類數據庫的schema.yml。

所以當我現在做$employee->getOrganization()(假設$員工是班級員工),我希望得到一個班級組織的對象。但是我得到一個字符串與組織的id字段的內容。 當我以相反方式嘗試時:$organization->getEmployee()(假設$組織是班級組織),它會向所有員工返回一個Doctrine_Collection。如何getOrganization()返回組織對象?

回答

2

它不起作用,因爲您的本地字段和關係具有相同的名稱(「組織」)。

這是更好地遵循教義命名「準則」:

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()

+0

不要被格雷德重申「組織」爲「組織」(在北美以外的首選拼寫) - 這是無關緊要的。 –

+0

@Colin Fine:哈哈,好吧,但我在NA之外,更喜歡z。 :P – Mathias

+0

@Colin oops ...抱歉... ;-)甚至不知道z被允許... –

相關問題