doushangxianq07480 2011-09-04 15:47
浏览 98
已采纳

Doctrine2:具有多个外键的表的关联映射

A friend of mine and me are working on a multilingual web-application for storing recipes. We use Doctrine 2.1.1 for persistence. At the moment I stuck generating models for an entity and hope someone can give me a hint, or show up a best practice for our case.

I'll try to explain our ERD broadly. Feel free to ask, if there is something unintelligible. Our application has the Entities recipe, ingredients, units (measure units for ingredients) and categories. Each of these entities is stored in an own table. Each entity can be translated in multiple languages. Storage of the translations is intended in a table called (you name it) translations. Now it will be a bit tricky… we have an extra table called mnemonic. We use it for identifying recipes, ingredients, categories and units of measure in a global context… best analogy to this is a GUID I think. The mnemonic also helps us to map between recipes, ingredients etc. and their translations. The mnemonic table consists of five rows: id (the primary key) and four additional meta-data rows: ingredient_id, recipe_id, category_id and unit_id referencing a 1:1 relationships to primary key of their relatives.

I'm asking myself how cam I map the relationship between the mnemonic entity and recipes, ingredients an so on. Or (to be more specific) how can the category_id in the mnemonic table be automatically populated with the value of the the primary key of the category table?

I tried first this kind of association mapping in my Category model:

class Category
{
/**
 * @var integer $id
 *
 * @Column(name="id", type="bigint", nullable=false)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 * @OneToOne(targetEntity="Mnemonic", mappedBy="category")
 * @JoinColumn(name="id", referencedColumnName="category_id")
 */
private $id;

and in the Mnemonic model:

class Mnemonic
{
/**
 *
 * @OneToOne(targetEntity="Category", inversedBy="mnemonic")
 * @JoinColumn(name="category_id", referencedColumnName="id")
 */    
private $categoryId;

That didn't work out — Doctrine dont' produce any errors, but populates only the category table. So I thought I could write some code in the __construct method of the Category model, where I would create a Mnemonic object, set his categoryId according to the ID of Category model and bind it to doctrine's entity manager. But that's kind of ugly — I think model classes should not be responsible for persistence. During the writing of my question I thought, that the solution could be a kind of a Factory class. It would take all the persistence logic out of the model and could handle special cases.

What do you think? What is the best solution?

Best regards in advice

Paul

  • 写回答

1条回答 默认 最新

  • duanshan5259 2012-07-06 19:00
    关注

    Have you thought about adding an event listener for onFlush? so essentially on flush, you check what object you are persisting, then execute some code such as creating the Mnemonic object.

    Info about registering onFlush events can be found on the doctrine website at the following link:

    http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/events.html#onflush

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题