这是我的第一篇文章,因为直到现在我一直通过您的答案找到解决方案,直到现在......
我有三个实体(自行车、类别和额外),它们有一些可翻译的字段。我知道 symfony 中有一个可翻译的扩展,但我发现有点晚了,我的解决方案一直在工作。
问题是我还有 3 个其他表(BikeI18N、CategoryI18N 和 ExtraI18N)。这些表与它们关联的列bike_id(例如)相关联。在这里,您有 Bike 和 Category 的架构及其 I18N 表:
自行车:
BEM\BikeBundle\Entity\Bike:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
marca:
type: string
length: 255
model:
type: string
length: 255
preu1Dia:
type: float
column: preu_1dia
[...]
manyToOne:
category:
targetEntity: BEM\CategoryBundle\Entity\Category
inversedBy: bikes
oneToMany:
translation:
targetEntity: BikeI18N
mappedBy: bike
cascade: [persist]
pedals:
targetEntity: Pedal
mappedBy: bike
talles:
targetEntity: Talla
mappedBy: bike
manyToMany:
accessoris:
targetEntity: Accessori
inversedBy: bikes
lifecycleCallbacks: { }
自行车I18N:
BEM\BikeBundle\Entity\BikeI18N:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
descripcio:
type: text
locale:
type: string
length: '2'
manyToOne:
bike:
targetEntity: Bike
inversedBy: translation
joinColumn:
bike_id:
referencedColumnName: id
lifecycleCallbacks: { }
类别:
BEM\CategoryBundle\Entity\Category:
type: entity
table: null
repositoryClass: BEM\CategoryBundle\Repository\CategoryRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
translation:
targetEntity: CategoryI18N
mappedBy: category
cascade: [persist]
bikes:
targetEntity: BEM\BikeBundle\Entity\Bike
mappedBy: category
lifecycleCallbacks: { }
最后是 CategoryI18N:
BEM\CategoryBundle\Entity\CategoryI18N:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
categoryId:
type: integer
column: category_id
locale:
type: string
length: '2'
translation:
type: string
length: 255
manyToOne:
category:
targetEntity: Category
inversedBy: translation
joinColumn:
category_id:
referencedColumnName: id
lifecycleCallbacks: { }
在我的“新”表单中,我有一个包含 4 个嵌入 CategoryI18N 表单的类别表单。它工作正常。问题是即使我做了同样的事情,Bike 和 BikeI18N 也没有。
如果我写$translation->setAccessori($this);
,Bike::addTranslation(BikeI18N $translation)
我得到错误传递给选择字段的实体必须被管理。也许将它们保留在实体管理器中?.
如果我不写$translation->setAccessori($this);
表单被保存但关系丢失,或者如果我将数据库设置为not_nullable bike_id
我得到一个错误。
对我来说毫无意义的最令人惊讶的事情是我setCategory($this)
在类 Category 的 addTranslation 中......有什么想法吗?