【问题标题】:Doctrine Get Entities Based on Selected Category ChildrenDoctrine 根据选定的类别子项获取实体
【发布时间】:2014-10-22 17:19:35
【问题描述】:

我想获取与所选类别的孩子相关的记录。

例如,我们有一个这样的类别树:

主要:

  1. 水果
  2. 汽车

孩子:(父母ID不能为空!在本例中:父母ID = 1)

  1. 苹果
  2. 橙色
  3. 香蕉

我们有一个像这样的实体(数据库记录):

id = 1, category_id = 2, title = This records must belong to ORANGE category!

当我们从数据库中选择水果类别时,我必须获取与水果本身相关的实体及其子类别。

我试过什么?

$qb
->select('i.id, i.title, i.slug, i.created_at, c.file')
->from('CSImageBundle:Image', 'i')
->leftJoin('i.taxonomies', 'tx')
->leftJoin('tx.children', 'st')
->where($qb->expr()->in('tx.id', 'st.id'));

失败...

我应该尝试什么?

【问题讨论】:

  • 它是如何失败的?,您能否提供错误或异常消息,也可以提供您的主要和孩子的映射。是那些不同的实体还是孩子在同一个实体上引用了它的父母?

标签: php mysql symfony doctrine-orm query-builder


【解决方案1】:

很难说没有错误消息,但在您的选择中似乎有问题,c.file 在哪里?

$qb
->select('i.id, i.title, i.slug, i.created_at, c.file') //should this be i.file? or tx.file? or st.file?
->from('CSImageBundle:Image', 'i')
->leftJoin('i.taxonomies', 'tx')
->leftJoin('tx.children', 'st')
->where($qb->expr()->in('tx.id', 'st.id'));

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多