Symfony2, Propel fixtures and concrete inheritance
-
25-06-2021 - |
Frage
I got an issue with Propel fixtures loading in Symfony2. I have the following schema:
<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>
The model builds correctly. The issue raises when I try to load the following fixtures:
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
The following error occurs:
[Propel] Exception
Cannot insert a value for auto-increment primary key (application.ID)
I added my "first_app" application twice (one in Application, another time in IphoneApplication) to prevent from another issue with my IphoneApplicationIdentifier fixture:
[Propel] Exception The object "first_app" from class "Acme\MyBundle\Model\Application" is not defined in your data file.
How can I insert fixtures for a concrete inheritant model?
Lösung
Propel is telling you that you are trying to set a value for an autoincrement column, this is due to the
id: first_app
line.
If I am understanding you correctly, you just want to add an Iphoneapplication item, is that right? If that is the case you just need to do:
Acme\MyBundle\Model\IphoneApplication:
first_app_iphone:
name: "MyFirstApp"
store_id: 2342
Andere Tipps
I had similar situation and ended up with having different schema files. For testing one - I removed auto-increment from schema and defined another place for storing models. It was long time ago, but general steps were as below:
Dump fixtures for prod environment
app/console propel:fixtures:dump
Build everything you need for testing database
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
Imported fixtures into test database
app/console propel:fixtures:load --env=test
Note, that commands may vary depending on version of Propel
I found the mistake by explaining my problem in details. The issue did not come from this fixtures file. The error I had seemed strange to me. Why the error mentioned *first_app* instead of *iphone_first_app*?
After digging into the other fixtures files, I found a forgotten reference to *first_app*. The solution is simply the following (as mentioned by 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
There is no need to define base class. :)