2012-08-07 88 views
3

我在Symfony2中加載了Propel裝置問題。我有以下架構:Symfony2,Propel裝置和混凝土繼承

<table name="application" phpName="Application"> 

    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> 
    <column name="name" type="varchar" required="true" primaryString="true" /> 

    <behavior name="timestampable" /> 

</table> 

<table name="application_iphone" phpName="IphoneApplication"> 

    <column name="store_id" type="varchar" required="true" /> 

    <behavior name="concrete_inheritance"> 
     <parameter name="extends" value="application" /> 
    </behavior> 

</table> 

<table name="application_identifier" phpName="IphoneApplicationIdentifier"> 

    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> 
    <column name="application_id" required="true" type="integer" /> 
    <column name="identifier" type="varchar" required="true" primaryString="true" /> 

    <foreign-key foreignTable="application_iphone"> 
     <reference local="application_id" foreign="id" />         
    </foreign-key>  

</table> 

模型構建正確。這個問題引起了當我嘗試加載以下夾具:

Acme\MyBundle\Model\Application: 
    first_app: 
     name: "MyFirstApp" 
     descendant_class: "IphoneApplication" 

Acme\MyBundle\Model\IphoneApplication: 
    first_app_iphone: 
     id: first_app 
     store_id: 2342 

Acme\MyBundle\Model\IphoneApplicationIdentifier:  
    first_app_identifier: 
     identifier: "com.acme.myfirstapp.appstore" 
     application_id: first_app_iphone 

出現以下錯誤:

[行走]異常
無法插入自動增量主鍵的值(application.ID )

應用加入我的 「first_app」 申請兩次(一個,另一個時間在IphoneApplication)從另一個問題,防止與我IphoneApplicationIdentifier夾具:

[行走]異常來自 類對象「first_app」「的Acme \ MyBundle \型號\應用程序」是不是在你的數據文件中定義。

如何爲混凝土繼承模型插入燈具?

+0

有同樣的問題...你有沒有嘗試從燈具中刪除ID? – 2012-08-07 15:14:26

+0

是的,我已經嘗試過。但是,如果我刪除它,我有兩個應用程序(而不是一個)插入到數據庫中:基本一個,另一個對應於iPhone。 :/ – 2012-08-07 15:19:05

回答

3

行走是告訴你,你正試圖將其設置爲自動增量列的值,這是由於

id: first_app 

line。

如果我正確理解你,你只是想添加一個iPhone應用程序項目,是嗎?如果是這種情況,你只需要做:

Acme\MyBundle\Model\IphoneApplication: 
    first_app_iphone: 
     name: "MyFirstApp" 
     store_id: 2342 
+0

我不確定在答案中是否清楚,但是您不需要爲fixtures文件中的應用程序表添加任何內容,由於表繼承而自動填充 – 2012-08-07 15:53:51

+0

我同意你的意見。如果只有這兩類,一切都會好起來的。但是,我沒有提到另一種模式,與我的孩子班的外鍵相關聯(我編輯了我的問題)。 問題是_IphoneApplicationIdentifier_似乎期待_Application_對象,而不是_IphoneApplication_之一在這種情況下。這就是爲什麼我提到了兩次我的「first_app」應用程序。 – 2012-08-07 15:58:53

+0

問題修復!問題是由另一個夾具文件造成的。感謝幫助我指出。 :) – 2012-08-07 16:13:19

1

我有類似的情況,並結束了不同的模式文件。爲了測試一個 - 我從模式中刪除了自動增量,併爲存儲模型定義了另一個地方。那是很久以前的事,但一般的步驟是如下:


  1. 爲督促環境

    app/console propel:fixtures:dump 
    
  2. 建立你需要的測試數據庫

    app/console propel:database:create --env=test 
    app/console propel:sql:build --env=test 
    app/console propel:sql:insert --force --env=test 
    app/console propel:model:build --env=test 
    
  3. 一切轉儲燈具

    將測試數據庫導入燈具

    app/console propel:fixtures:load --env=test 
    

。注意,命令可能會因行走版本而異

0

我發現了錯誤,詳細解釋我的問題。這個問題不是來自這個燈具文件。我對這個錯誤似乎很陌生。爲什麼提到的錯誤* first_app *而不是* iphone_first_app *?

挖掘到其他燈具文件後,我發現一個被遺忘的* first_app *的引用。解決辦法簡單如下(由Carlos Granados提到):

Acme\MyBundle\Model\IphoneApplication: 
    first_app_iphone: 
     name: "My first app" 
     store_id: 2342 

Acme\MyBundle\Model\IphoneApplicationIdentifier:  
    first_app_identifier: 
     identifier: "com.acme.myfirstapp.appstore" 
     application_id: first_app_iphone 

沒有必要定義基類。 :)