douliaodun9153 2015-07-17 00:41
浏览 32

MySQL Doctrine YAML

What I want is get the product and the product should be something like this:

{
  idProduct: 1,
  mainCategory: 1, // One of the categories is the main one.
  categories: [1, 2, 3],
  name: "John" //and so on.. the important thing is mainCategory...
}

Im using Doctrine (at work) with YAML, and I know I could use something like this:

Product:
  type: entity
  manyToMany:
    groups:
      targetEntity: Category
      joinTable:
        name: products_categories
        joinColumns:
          product_id:
            referencedColumnName: id
        inverseJoinColumns:
          category_id:
            referencedColumnName: id

That would create something like this three tables (use your imagination xD):

**Product**
id

**Category**
id

**Category_Product**
id_product
id_category

But I need mainCategory in the product too:

**Product**
id
main_category #this is important and my problem.

What can I do to achieve this... I put an object example in json because is easy to understand... but Im using PHP at work, with Doctrine.. I guess Doctrine2, Im not sure about that.

At work I have in production category and product already working but in a one to one relationship... today one product can have only one category, my issue is change that, because one product can have several categories but one is the main one.

Thanks in advance!!!

Edit: Someone at work suggest me, a third entitie, but Im not sure about that.

  • 写回答

1条回答 默认 最新

  • douzhang7184 2015-07-17 03:42
    关注

    Maybe you can think outside of the box a little and define mainCategory as just an ID. This ID can be used to look up categories. Instead of calling mainCategory via regular doctrine, write a custom function that would get the mainCategory object from this mainCategory ID.

    Yes, it is manual work, but doctrine does not natively support your scenario. The most important thing I would worry about is making sure mainCategory and Categories stay in sync such that you can never have a mainCategory that is not in category. You can do this easily though if you always check that mainCategory is in Categories on save.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集