0

我創建了一個ER模型爲我的項目和PHP實現它,並增加了以下maaping信息:我無法堅持一個對象,使用學說,具有包括一個複合主鍵的外鍵

AppBundle\Entity\Competition: 
    type: entity 
    id: 
     id: 
      type: integer 
      generator: 
       strategy: AUTO 
    fields: 
     name: 
     location: 
     date: 
      type: datetime 
    lifecycleCallbacks: { } 
    oneToMany: 
     runs: 
      targetEntity: Run 
      mappedBy: comp 
      cascade: [persist] 

AppBundle\Entity\Run: 
    type: entity 
    id: 
     id: 
      type: integer 
     comp: 
      associationKey: true 
    fields: 
     name: 
    lifecycleCallbacks: { } 
    manyToOne: 
     comp: 
      targetEntity: Competition 
      inversedBy: runs 

AppBundle\Entity\Participate: 
    type: entity 
    id: 
     athlete: 
      associationKey: true 
     run: 
      associationKey: true 
     comp: 
      associationKey: true 
    fields: 
     number: 
      type: integer 
    lifecycleCallbacks: { } 
    manyToOne: 
     athlete: 
      targetEntity: Athlete 
      cascade: [persist] 
     run: 
      targetEntity: Run 
      cascade: [persist] 
     comp: 
      targetEntity: Run 
      cascade: [persist] 

編輯:運行應該是我們AK實體,所以我認爲我需要2個關係來運行。 Run首先是跑步本身,第二次是比賽,Run是屬於的。

競爭合作,運行得好好的,我能堅持,並獲取它們,但只要我努力堅持參與的對象,我得到以下錯誤:

Binding an entity with a composite primary key to a query is not supported. You should split the parameter into the explicit fields and bind them separately.

我用以下代碼:

$em = $this->getDoctrine()->getManager(); 
$em->persist($participate); 
$em->flush(); 

我不知道該怎麼辦才能解決此問題。

感謝

EDIT2:

我理解,從理論上說,我不需要關係compParticipate,因爲它已經在RunRun對象是唯一的。但是,如果我想相應地更新我的數據庫,學說給了我下面的SQL:

ALTER TABLE Participate DROP FOREIGN KEY FK_8B9E3EEF4D0D3BCB; 
DROP INDEX IDX_8B9E3EEF4D0D3BCB ON Participate; 
ALTER TABLE Participate DROP PRIMARY KEY; 
ALTER TABLE Participate DROP comp_id; 
ALTER TABLE Participate ADD PRIMARY KEY (athlete_id, run_id); 

這將刪除外鍵CompetitionParticipate。但run_id沒有被定義爲唯一的,因爲它應該是一個弱關鍵。

回答

0

這可能是因爲2個多對一關係具有相同的實體targetEntity

AppBundle\Entity\Participate 
    manyToOne: 
    run: targetEntity: Run 
    comp: targetEntity: Run 

比較應該有targrtEntity「競爭」。

+0

但是這會使參與和競爭有關係。然而,參與應該只與Run有關係,Run有一個複合PK – exastion

+0

那麼爲什麼你不創建一箇中間實體來避免使用它兩次呢? – user3504263

+0

我真的不明白。我在問題中添加了一些信息。 – exastion

相關問題