Yii CSqlDataProvider混淆

Yii CSqlDataProvider混淆,yii,Yii,我在理解它以及它是如何工作的方面遇到了一些困难 当我使用时,可以按如下方式访问结果: $data->userProfile['first_name']; $data['userProfile']['first_name']; $data['author']['first_name']; 但是,当我使用CSqlDataProvider时,我知道结果是作为数组而不是对象返回的。然而,阵列的结构是扁平的。换句话说,我看到以下数组: $data['first_name'] 而不是 $dat

我在理解它以及它是如何工作的方面遇到了一些困难

当我使用时,可以按如下方式访问结果:

$data->userProfile['first_name'];
$data['userProfile']['first_name'];
$data['author']['first_name'];
但是,当我使用CSqlDataProvider时,我知道结果是作为数组而不是对象返回的。然而,阵列的结构是扁平的。换句话说,我看到以下数组:

$data['first_name'] 
而不是

$data['userProfile']['first_name']
但这里的问题是,如果在我的sql代码中有另一个包含first_name字段的联接表(我们称之为“author”),该怎么办?使用CActiveDataProvider可以消除这两个字段的歧义,因此我可以执行以下操作来访问这两个字段:

$data->userProfile['first_name'];
$data->author['first_name'];
但使用CSqlDataProvider,似乎没有任何方式可以访问数据,如下所示:

$data->userProfile['first_name'];
$data['userProfile']['first_name'];
$data['author']['first_name'];
因此,除了直接在我的SQL中为这些字段指定一个唯一的名称外,还可以执行以下操作:

select author.first_name as author_first_name, userProfile.first_name as user_first_name
然后像这样提到他们:

select author.first_name as author_first_name, userProfile.first_name as user_first_name
$data['author_first_name']; $data['user\u first\u name']

是否有任何方法可以让CSqlDataProvider自动构造数组,使它们以与CActiveDataProvider对象相同的方式嵌套?这样我就可以使用$data['userProfile']['first_name']调用它们

或者我是否应该使用另一个类来获取这些嵌套数组


非常感谢

据我所知,没有任何Yii-DB方法像您所寻找的那样将查询结果拆分为二维数组。我认为您需要——正如您所建议的那样——在select语句中为列名加上别名

当您在查询中连接表时,MySql返回一行数据,而CSqlDataProvider返回的正是MySql所做的:由列名索引/键入的单个表格数组表示,就像您的查询返回的一样

如果您想将结果拆分为多维数组,我可以为列别名,或者使用常规数组(您仍然可以通过该数组传递复杂的查询和联接)