开发者

Symfony doctrine i18n behavior data-load failure: Integrity constraint violation

开发者 https://www.devze.com 2023-02-04 00:39 出处:网络
I want tho have I18N categories table. I have followed the jobeet example. Schema and fixtures data below.

I want tho have I18N categories table. I have followed the jobeet example.

Schema and fixtures data below.

./symfony doctrine:build --db --all-classes --and-migrate
./symfony doctrine:data-load data/fixtures/category.yml

When I run these commands no data gets inserted into database and red box with failure displays: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-et' for key 'PRIMARY'

Queries that get executed on data-load task (logged with "mysqld --log=logfile"):

1 Query DELETE FROM my_category
2 Query START TRANSACTION
3 Query SELECT k.id AS k__id, k.position AS k__position FROM my_category k ORDER BY k.position desc LIMIT 1
4 Query INSERT INTO my_category (gender, position, created_at, updated_at) VALUES ('female', '1', '2011-01-14 12:33:05', '2011-01-14 12:33:05')
5 Query SELECT k.id AS k__id, k.lang AS k__lang, k.slug AS k__slug FROM my_category_translation k WHERE (k.slug LIKE 'name-value%' AND k.lang = 'et' AND k.name = 'Name value')
6 Query INSERT INTO my_category_translation (id, lang, name, seeking_label, seeking, slug) VALUES ('1', 'et', 'Name value', 'Label value', 'Value', 'name-value')
7 Query SELECT k.id AS k__id, k.lang AS k__lang, k.slug AS k__slug FROM my_category_translation k WHERE (k.slug LIKE '%' AND k.lang = 'et_EE' AND k.name IS NULL)
8 Query INSERT INTO my_category_translation (id, lang, slug) VALUES ('1', 'et_EE', '')
9 Query rollback
10 Quit 

When I manually run these commands on freshly built database. Queries from 3 to 6 above run fine and insert data. Query nr. 7 does not output data and query nr. 8 is the failing one. (Because id 1 already exists, inserted in step 6).

The problem must be in query nr. 7. "k.lang = 'et_EE'" I'm pretty sure it should be 'et'? Why query nr. 7 is neccessary seems exactly like query nr. 5 but without and/or messed up values?

What I have gotten worng or is it a bug?

schema.yml

myCategory:
  actAs:
    Timestampable: ~
    Sortable:      ~
    I18n:
      fields:       [name, seeking_label, seeking]
      actAs:
        Sluggable:
          fields:   [name]
          uniqueBy: [lang, name]
          builder:  [mySluggableTranslit, urlize]
  columns:
    name:          { type: string(255), notnull: true 开发者_开发知识库}
    gender:        { type: string(6)   }
    seeking_label: { type: string(255) }
    seeking:       { type: string(255) }

data/fixtures/category.yml

myCategory:
  category-1:
    gender: female
    position: '1'
    Translation:
      et:
        name: 'Name value'
        seeking_label: 'Label value'
        seeking: 'Value'


By default, the lenght of the culture field in i18n tables is 2. You have to change it to 5 if you want to use long culture codes. Otherwise, 'et_EE' will be truncated to 'et'.

I18n:
  fields:       [name, seeking_label, seeking]
  length: 5

See: http://www.doctrine-project.org/documentation/manual/1_0/en/behaviors#core-behaviors:i18n


Got solution: Sortable behavior is not mixing well with I18n.

If you want to save yourself from 3 days of debugging then don't mix Sortable "csDoctrineActAsSortablePlugin" behavior with I18n behavior.

I'll just drop sortable support for now. Any reccomendations for Sortable bahavior that works with I18n is welcome.


This could help some new Doctrine explorers: ID columns, used to identify I18n classes should be set as autoincrement but never as unique, because this unique constraint will be expanded to the translation tables, so fixtures won't be inserted as expected.

In example:

Gender:
  female:
    Translation:
      en:
        short_name: F
        name: Female
      es:
        short_name: F
        name: Femenino
  male:
    Translation:
      en:
        short_name: M
        name: Male
      es:
        short_name: M
        name: Masculino

If the Gender table (class) has and id as autoincrement, and unique, this fixture insertion would fail. So don't use unique in autoincrement fields, used as keys in translation tables (I18n behavior).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号