【问题标题】:Symfony2, Propel fixtures and concrete inheritanceSymfony2,推进装置和具体继承
【发布时间】:2012-08-04 15:26:47
【问题描述】:

我在 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”应用程序(一次在 Application 中,另一次在 IphoneApplication 中)以防止我的 IphoneApplicationIdentifier 出现另一个问题夹具:

[Propel] 异常来自类的对象“first_app” “Acme\MyBundle\Model\Application”未在您的数据文件中定义。

如何为具体的继承模型插入固定装置?

【问题讨论】:

  • 遇到了同样的问题...您是否尝试从灯具中删除 id?
  • 是的,我已经尝试过了。但是,如果我删除它,我会在数据库中插入两个应用程序(而不是一个):基本应用程序和与 iPhone 对应的另一个应用程序。 ://

标签: php symfony propel fixtures concrete-inheritance


【解决方案1】:

Propel 告诉您您正在尝试为自动增量列设置值,这是由于

    id: first_app

线。

如果我对您的理解正确,您只是想添加一个 Iphoneapplication 项目,对吗?如果是这种情况,您只需要这样做:

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

【讨论】:

  • 我不确定答案是否清楚,但是您不需要在fixtures文件中为应用程序表添加任何内容,由于表继承,​​这是自动填充的
  • 我同意你的看法。如果只有这两个班,一切都会好起来的。但是,我没有提到另一个模型,它与我的子类的外键相关联(我编辑了我的问题)。问题是 IphoneApplicationIdentifier 似乎期望一个 Application 对象,而不是在这种情况下一个 IphoneApplication 对象。这就是为什么我两次提到我的“first_app”应用程序。
  • 问题已解决!问题是由另一个夹具文件引起的。谢谢帮我指出来。 :)
【解决方案2】:

我遇到了类似的情况,最终得到了不同的架构文件。为了测试一个 - 我从模式中删除了自动增量并定义了另一个存储模型的地方。很久以前了,但一般步骤如下:


  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
    

请注意,命令可能因 Propel 版本而异

【讨论】:

    【解决方案3】:

    我通过详细解释我的问题找到了错误。该问题并非来自此固定装置文件。我的错误对我来说似乎很奇怪。为什么错误提到 *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
    

    不需要定义基类。 :)

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2019-06-08
      • 2020-12-16
      • 2011-04-19
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 2013-01-21
      相关资源
      最近更新 更多