我創建了一個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:
我理解,從理論上說,我不需要關係comp
在Participate
,因爲它已經在Run
和Run
對象是唯一的。但是,如果我想相應地更新我的數據庫,學說給了我下面的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);
這將刪除外鍵Competition
在Participate
。但run_id
沒有被定義爲唯一的,因爲它應該是一個弱關鍵。
但是這會使參與和競爭有關係。然而,參與應該只與Run有關係,Run有一個複合PK – exastion
那麼爲什麼你不創建一箇中間實體來避免使用它兩次呢? – user3504263
我真的不明白。我在問題中添加了一些信息。 – exastion