Yii - 使用活动记录连接多个表

时间:2014-06-29 04:50:34

标签: php mysql sql yii

我目前正致力于YII申请,我必须承认这是我的第一次尝试。我很难掌握YII的活动记录组件。特别是我想使用with()加入三个表。

我的mysql表如下:

video_specific
- id (primary key)
- random_id
- user_id
- video_link
- quality (enum, high,low)

video_details
- video_id
- upvote_count
- downvote_count
- timestamp

ladder_videos
- ladder_id
- video_id

ladder_specific
- id
- random_id
- name
- description
- ladder_type
- status
- video_count

因此,在使用gii工具后,我给出了具有以下关系的模型。请注意,我没有为ladder_videos表创建模型。

在videoSpecific模型中

'ladderSpecifics' => array(self::MANY_MANY, 'LadderSpecific', 
'ladder_videos(video_id, ladder_id)'),
'videoDetails' => array(self::HAS_ONE, 'VideoDetails', 'video_id')

在ladderSpecific模型中

'videoSpecifics' => array(self::MANY_MANY, 'VideoSpecific', 
'ladder_videos(ladder_id, video_id)')

通过这些关系,我认为我可以正确地进行以下查询

$ladders = LadderSpecific::model()->with(
        array('videoSpecifics'=>array('select'=>'id,video_link,random_id',
        'join'=>'videoDetails')))->findAll();

但是我收到以下错误

    CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 'videoDetails' 
at line 1. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`random_id` AS 
`t0_c1`, `t`.`name` AS `t0_c2`, `t`.`description` AS `t0_c3`, `t`.`ladder_type` AS 
`t0_c4`, `t`.`status` AS `t0_c5`, `t`.`video_count` AS `t0_c6`, `videoSpecifics`.`id` AS 
`t1_c0`, `videoSpecifics`.`video_link` AS `t1_c3`, `videoSpecifics`.`random_id` AS `t1_c1` 
FROM `ladder_specific` `t` LEFT OUTER JOIN `ladder_videos` `videoSpecifics_videoSpecifics` 
ON (`t`.`id`=`videoSpecifics_videoSpecifics`.`ladder_id`) LEFT OUTER JOIN `video_specific` 
`videoSpecifics` ON (`videoSpecifics`.`id`=`videoSpecifics_videoSpecifics`.`video_id`) 
videoDetails 

任何想法为什么?请将您的答案限制为活动记录,而不是DAO或查询构建器。感谢

2 个答案:

答案 0 :(得分:0)

有助于回顾关系如何在Yii中发挥作用。

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

在查询模型时,Yii会自动生成查询以获取关系数据,因此您无需提供它。

因此,您必须提供给您关系的名称。

$ladders = LadderSpecific::model()->with('videoSpecifics')->findAll();

答案 1 :(得分:0)

所以答案是更新ladderSpecific关系。所以一旦你更新它们就应该这样:

public function relations(){
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'ladderDetails' => array(self::HAS_ONE, 'LadderDetails', 'ladder_id'),
            'videoSpecifics' => array(self::MANY_MANY, 'VideoSpecific', 'ladder_videos(ladder_id, video_id)'),
                                array(self::HAS_ONE, 'VideoDetails', 'video_id'),
        );
    }

更新此关系后,您可以执行以下操作:

$ladders = LadderSpecific::model()->with(
                                        array('videoSpecifics.videoDetails'=>
                                            array(
                                                'order'=>'videoDetails.upvote_count - videoDetails.downvote_count DESC',
                                                'limit'=>'1'
                                            )))->findAll(array('order'=>'name',));

唯一的问题是出于某种原因我无法限制返回的记录数量。一旦我弄明白,我会更新我的答案。如果你有更好的方法,请告诉我。

相关问题