drba1172 2015-10-21 02:00
浏览 108

复杂的CDbCriteria使用'with'子句

I have 3ish tables (there's actually 4 because a joining table for a many-many relationship).

Lets say they are: grandparent(g), parent(p), child(c), with gp_join(gp) for the joining table for the grandparent-parent many-many relation. With the following relations:

Grandparent Relations:

'parent' => array( self::MANY_MANY, 'Parent', 'gp_join(g_id, p_id)' )

Parent Relations:

'grandparent' => array( self::MANY_MANY, 'Grandparent', 'gp_join(p_id, g_id)' ),
'child' => array( self::HAS_MANY, 'Child', 'p_id' )

Child Relations:

'parent' => array( self::BELONGS_TO, 'Parent', 'p_id' )

I want to find all grandchildren of one grandparent. I have an SQL query that does this successfully, but I need to know how to do this using the Yii framework (preferably using the 'with' property because it's cool).

Working SQL statement:

SELECT c.name
FROM grandparent `g`
JOIN gp_join `gp` ON gp.g_id=g.g_id
JOIN parent `p` ON p.p_id=gp.p_id
JOIN child `c` ON c.p_id = p.p_id
WHERE g.g_id=12;

Here is what I have been trying (it returns every child regardless of whether they have the right grandparent):

$criteria = new CDbCriteria();
$criteria->with['parent'] = array (
    'with' => array(
            'grandparent' => array( 
            'alias' => 'g',
            'condition' => 'g.g_id='.$this->g_id
        )
     ),
    'alias' => 'p',
    'condition' => 'p.p_id=t.list_id'
);
return Child::model()->findAll($criteria);
  • 写回答

1条回答 默认 最新

  • dtdfj08626 2015-10-21 19:43
    关注

    I would recommend dropping gp_join table and connecting those three tables with MANY_MANY relation becouse you have such relation between parent and child already. Its better to make it consistent.
    I dont see your relations but I see the problem where you are trying to connect parent and grandparent without using gp_join table which will not work given conditions you have.
    Here's how it should look like

    $criteria->with['parent'] = array (
            'with' => array(
                'gpJoin' => array(
                    'with'=> array(
                        'grandparent'=> array(
                            'alias' => 'g',
                            'condition' => 'g.g_id=:g_id',
                            'params'    => array(':g_id' => $this->g_id),
                        )
                    ),
                )
            ),
        );
    

    I added gpJoin relation and changed condition so it's not prone to SQL injection.

    Child relations

    'parent'    => array(self::MANY_MANY, "Parents", 'parent(p_id, p_id)'),
    

    Parent relations

    'gpJoin'    => array(self::HAS_MANY, 'GpJoin', 'p_id'),
    

    GpJoin relations

    'parent'    => array(self::HAS_ONE, 'Parents', 'p_id'),
    'grandparent'   => array(self::BELONGS_TO, 'Grandparent', 'g_id'),
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)