我在“ext_translations”表中有一个包含所有翻译的表。
翻译效果很好。现在的问题是:我想通过 sonata-admin bundle 管理这些翻译。
我已经找到了一份文档,说明如何使用 sonata admin 获得工作学说扩展。但就我而言,我的所有翻译(针对多个实体)都有一个表/实体。
所以根据这个文档:http://www.elao.com/blog/symfony-2/doctrine-2/how-to-manage-translations-for-your-object-using-sonataadminbundle.html我的 mappedBy 属性应该是什么(见下文)?
ext_translations 表:
mysql> show columns from ext_translations;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| locale | varchar(8) | NO | MUL | NULL | |
| object_class | varchar(255) | NO | | NULL | |
| field | varchar(32) | NO | | NULL | |
| foreign_key | varchar(64) | NO | | NULL | |
| content | longtext | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
映射者:
/**
* @ORM\OneToMany(targetEntity="ProfileTranslation", mappedBy="object", cascade={"persist", "remove"})
*/
protected $translations;
据我了解这里的问题:“我有一个组合键(对象类(实体)+名称(属性的)+ foreignKey(实体的 id)),那么'mappedBy'应该如何引用这个?
我不想为每个可翻译实体创建一个额外的类(就像上面的教程一样)
最佳答案
你的情况很复杂。如果您不使用任何注释,可能是最好的,override the repository class ,并构建您自己的所有逻辑。
正如 Feras 在评论中所说,我们可以尝试利用自 Doctrine 2.1 以来创建复合复合键作为主键的新能力。
Doctrine 2 supports composite primary keys natively. Composite keys are a very powerful relational database concept and we took good care to make sure Doctrine 2 supports as many of the composite primary key use-cases. For Doctrine 2.0 composite keys of primitive data-types are supported, for Doctrine 2.1 even foreign keys as primary keys are supported.
在文档中,我们有一个与您的用例或多或少相似的用例的好例子:
Dynamic Attributes of an Entity (for example Article). Each Article has many attributes with primary key “article_id” and “attribute_name”.
但由于该方法只考虑一个实体,因此我们必须根据您的需求对其进行调整。我们可以按照以下步骤:
为您的
ext_translations
表创建一种 ViewCREATE VIEW profile_ext_translations AS SELECT * FROM ext_translations WHERE object_class = 'Profile'
然后为该 View 创建不同的实体,即您将拥有一个具有复合主键的实体
ProfileExtTranslations
,如下所示:** * @Entity */ class ProfileExtTranslations { /** * @ORM\ManyToOne(targetEntity="Profile", inversedBy="translations") * @ORM\JoinColumn(name="foreign_key", referencedColumnName="id", onDelete="CASCADE")*/ private $profile; /** @Id @Column(type="string") */ private $field; //Other fields and methods }
现在,Profile 实体的代码,在翻译的 mappedBy 中,您只需使用:
/** * @ORM\OneToMany(targetEntity="ProfileExtTranslation", mappedBy="profile", cascade={"persist", "remove"}) */ protected $translations;
有了这个,可能还有一点调整,你应该让它工作。
关于php - 带有可翻译字段的 SonataAdminBundle(原则扩展),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624336/