1

所以,我对 yii 有点陌生,如果这听起来像一个蹩脚的问题,我很抱歉,但我有一些奇怪的行为。我正在尝试加载带有评论的博客文章,但 actionView 仅返回博客文章:

    public function actionView($id)
    {
           $model = $this->loadModel($id);
           $criteria = new CDbCriteria;
           $criteria->condition = 'postId = ' . $id;
           $criteria->order = 'commentTime desc';
           $criteria->with = array('user');
           $dataSet = new CActiveDataProvider('Comments', array(
             'criteria' => $criteria,
            )
           );
           echo "<pre>";
           print_r($dataSet);
           echo "</pre>";
           $this->render('view',array(
                    'model'=>$model,
                    'dataSet'=>$dataSet,

            ));
    }

现在查看文件:

$this->renderPartial('_view',array('data'=>$model,));
$this->renderPartial('comments',array('data'=>$dataSet,));

有趣的是,根据日志,它甚至没有从评论表中选择,但是当我 print_r 时,我得到了表的整个结构。

CActiveDataProvider Object
(
    [modelClass] => Comments
    [model] => Comments Object
        (
            [_md:CActiveRecord:private] => CActiveRecordMetaData Object
                (
                    [tableSchema] => CMysqlTableSchema Object
                        (
                            [schemaName] => 
                            [name] => Comments
                            [rawName] => `Comments`
                            [primaryKey] => id
                            [sequenceName] => 
                            [foreignKeys] => Array
                                (
                                    [postId] => Array
                                        (
                                            [0] => Posts
                                            [1] => id
                                        )

                                    [userId] => Array
                                        (
                                            [0] => users
                                            [1] => id
                                        )

                                )

                            [columns] => Array
                                (
                                    [id] => CMysqlColumnSchema Object
                                        (
                                            [name] => id
                                            [rawName] => `id`
                                            [allowNull] => 
                                            [dbType] => int(11)
                                            [type] => integer
                                            [defaultValue] => 
                                            [size] => 11
                                            [precision] => 11
                                            [scale] => 
                                            [isPrimaryKey] => 1
                                            [isForeignKey] => 
                                            [autoIncrement] => 1
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [postId] => CMysqlColumnSchema Object
                                        (
                                            [name] => postId
                                            [rawName] => `postId`
                                            [allowNull] => 
                                            [dbType] => int(11)
                                            [type] => integer
                                            [defaultValue] => 
                                            [size] => 11
                                            [precision] => 11
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 1
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [userId] => CMysqlColumnSchema Object
                                        (
                                            [name] => userId
                                            [rawName] => `userId`
                                            [allowNull] => 
                                            [dbType] => int(11)
                                            [type] => integer
                                            [defaultValue] => 
                                            [size] => 11
                                            [precision] => 11
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 1
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [commentTime] => CMysqlColumnSchema Object
                                        (
                                            [name] => commentTime
                                            [rawName] => `commentTime`
                                            [allowNull] => 
                                            [dbType] => timestamp
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 
                                            [precision] => 
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [commentText] => CMysqlColumnSchema Object
                                        (
                                            [name] => commentText
                                            [rawName] => `commentText`
                                            [allowNull] => 
                                            [dbType] => mediumtext
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 
                                            [precision] => 
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [voteUp] => CMysqlColumnSchema Object
                                        (
                                            [name] => voteUp
                                            [rawName] => `voteUp`
                                            [allowNull] => 
                                            [dbType] => mediumint(8) unsigned
                                            [type] => integer
                                            [defaultValue] => 0
                                            [size] => 8
                                            [precision] => 8
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [voteDown] => CMysqlColumnSchema Object
                                        (
                                            [name] => voteDown
                                            [rawName] => `voteDown`
                                            [allowNull] => 
                                            [dbType] => mediumint(8) unsigned
                                            [type] => integer
                                            [defaultValue] => 0
                                            [size] => 8
                                            [precision] => 8
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [reports] => CMysqlColumnSchema Object
                                        (
                                            [name] => reports
                                            [rawName] => `reports`
                                            [allowNull] => 
                                            [dbType] => smallint(5) unsigned
                                            [type] => integer
                                            [defaultValue] => 0
                                            [size] => 5
                                            [precision] => 5
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                )

                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )

                    [columns] => Array
                        (
                            [id] => CMysqlColumnSchema Object
                                (
                                    [name] => id
                                    [rawName] => `id`
                                    [allowNull] => 
                                    [dbType] => int(11)
                                    [type] => integer
                                    [defaultValue] => 
                                    [size] => 11
                                    [precision] => 11
                                    [scale] => 
                                    [isPrimaryKey] => 1
                                    [isForeignKey] => 
                                    [autoIncrement] => 1
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [postId] => CMysqlColumnSchema Object
                                (
                                    [name] => postId
                                    [rawName] => `postId`
                                    [allowNull] => 
                                    [dbType] => int(11)
                                    [type] => integer
                                    [defaultValue] => 
                                    [size] => 11
                                    [precision] => 11
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 1
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [userId] => CMysqlColumnSchema Object
                                (
                                    [name] => userId
                                    [rawName] => `userId`
                                    [allowNull] => 
                                    [dbType] => int(11)
                                    [type] => integer
                                    [defaultValue] => 
                                    [size] => 11
                                    [precision] => 11
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 1
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [commentTime] => CMysqlColumnSchema Object
                                (
                                    [name] => commentTime
                                    [rawName] => `commentTime`
                                    [allowNull] => 
                                    [dbType] => timestamp
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 
                                    [precision] => 
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [commentText] => CMysqlColumnSchema Object
                                (
                                    [name] => commentText
                                    [rawName] => `commentText`
                                    [allowNull] => 
                                    [dbType] => mediumtext
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 
                                    [precision] => 
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [voteUp] => CMysqlColumnSchema Object
                                (
                                    [name] => voteUp
                                    [rawName] => `voteUp`
                                    [allowNull] => 
                                    [dbType] => mediumint(8) unsigned
                                    [type] => integer
                                    [defaultValue] => 0
                                    [size] => 8
                                    [precision] => 8
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [voteDown] => CMysqlColumnSchema Object
                                (
                                    [name] => voteDown
                                    [rawName] => `voteDown`
                                    [allowNull] => 
                                    [dbType] => mediumint(8) unsigned
                                    [type] => integer
                                    [defaultValue] => 0
                                    [size] => 8
                                    [precision] => 8
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [reports] => CMysqlColumnSchema Object
                                (
                                    [name] => reports
                                    [rawName] => `reports`
                                    [allowNull] => 
                                    [dbType] => smallint(5) unsigned
                                    [type] => integer
                                    [defaultValue] => 0
                                    [size] => 5
                                    [precision] => 5
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                        )

                    [relations] => Array
                        (
                            [user] => CBelongsToRelation Object
                                (
                                    [joinType] => LEFT OUTER JOIN
                                    [on] => 
                                    [alias] => 
                                    [with] => Array
                                        (
                                        )

                                    [together] => 
                                    [scopes] => 
                                    [name] => user
                                    [className] => User
                                    [foreignKey] => userId
                                    [select] => *
                                    [condition] => 
                                    [params] => Array
                                        (
                                        )

                                    [group] => 
                                    [join] => 
                                    [having] => 
                                    [order] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [post] => CBelongsToRelation Object
                                (
                                    [joinType] => LEFT OUTER JOIN
                                    [on] => 
                                    [alias] => 
                                    [with] => Array
                                        (
                                        )

                                    [together] => 
                                    [scopes] => 
                                    [name] => post
                                    [className] => Posts
                                    [foreignKey] => postId
                                    [select] => *
                                    [condition] => 
                                    [params] => Array
                                        (
                                        )

                                    [group] => 
                                    [join] => 
                                    [having] => 
                                    [order] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                        )

                    [attributeDefaults] => Array
                        (
                            [voteUp] => 0
                            [voteDown] => 0
                            [reports] => 0
                        )

                    [_model:CActiveRecordMetaData:private] => Comments Object
 *RECURSION*
                )

            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array
                (
                )

            [_related:CActiveRecord:private] => Array
                (
                )

            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 
            [_alias:CActiveRecord:private] => t
            [_errors:CModel:private] => Array
                (
                )

            [_validators:CModel:private] => 
            [_scenario:CModel:private] => 
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )

    [keyAttribute] => 
    [_criteria:CActiveDataProvider:private] => CDbCriteria Object
        (
            [select] => *
            [distinct] => 
            [condition] => postId = 2
            [params] => Array
                (
                )

            [limit] => -1
            [offset] => -1
            [order] => commentTime desc
            [group] => 
            [join] => 
            [having] => 
            [with] => Array
                (
                    [0] => user
                )

            [alias] => 
            [together] => 
            [index] => 
            [scopes] => 
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )

    [_id:CDataProvider:private] => Comments
    [_data:CDataProvider:private] => 
    [_keys:CDataProvider:private] => 
    [_totalItemCount:CDataProvider:private] => 
    [_sort:CDataProvider:private] => 
    [_pagination:CDataProvider:private] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)
4

2 回答 2

2

您正在为 SQL 注入(或至少有可能)以这种方式构建您的标准做好准备。如果你做这样的事情,你会得到你的结果吗?

public function actionView($id)
{
       // the with will load your comments through your Blog relationship
       // with the comments and then user your Comment -> User relationship as well
       // doing it this way eliminates the need for creating your own criteria object
       $model = Blog::model()->with('comments.user')->findByPk((int)$id);

       // we then set a defaultOrder here
       $dataSet = new CActiveDataProvider($model->comments, array(
           'sort'=>array(
               'defaultOrder'=>'commentTime desc',
           ),
        )
       );
       echo "<pre>";
       print_r($dataSet);
       echo "</pre>";

       ....

}
于 2012-07-06T16:30:09.897 回答
1

试试这个模型调试器,然后测试你的输出,请把你的问题说清楚。http://www.kevinkorb.com/post/26

于 2012-07-06T05:16:34.277 回答