Yii的CGridview - 搜索/排序工作,但价值不被显示在各个小区 [英] Yii CGridview - search/sort works, but values aren't being displayed on respective cells

查看:194
本文介绍了Yii的CGridview - 搜索/排序工作,但价值不被显示在各个小区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我半沮丧与此Yii的CGridView问题,任何帮助或指导将是非常美联社preciated。

I am semi-frustrated with this Yii CGridView problem and any help or guidance would be highly appreciated.

我有两个相关的表店(shop_id主)和联系人(shop_id国外),使得单店可以有多个联系人。我使用CGridview拉记录和整理,并在商店模式我的关系,功能是一样的东西:

I have two related tables shops (shop_id primary) and contacts (shop_id foreign) such that a single shop may have multiple contacts. I'm using CGridview for pulling records and sorting and my relation function in shops model is something like:

  'shopscontact' => array(self::HAS_MANY, 'Shopsmodel', 'shop_id');

在店内格,我需要用的可接触的任何一个展示店行。我尝试对其进行过滤,搜索电网曾pretty的很好,但我被困在一个很奇怪的问题。各格列不显示预定的值。

On the shop grid, I need to display the shop row with any one of the available contacts. My attempt to filter, search the Grid has worked pretty fine, but I'm stuck in one very strange problem. The respective grid column does not display the value that is intended.

在CGridview文件,我正在做这样的事情

On CGridview file, I'm doing something like

  array(
    'name' => 'shopscontact.contact_firstname',
    'header' => 'First Name',        
    'value' => '$data->shopscontact->contact_firstname'
    ),

显示联系人的名字。然而,即使在该情况下,搜索/排序都工作(我发现通过检查db协会),格列出来空! :(当我做的var_dump

to display the contact's first name. However, even under circumstances that searching/sorting are both working (I found out by checking the db associations), the grid column comes out empty! :( And when I do a var_dump

 array(
    'name' => 'shopscontact.contact_firstname',
    'header' => 'First Name',
    'value' => 'var_dump($data->shopscontact)'
    ),

转储显示记录值在_private属性如下:

The dump shows record values in _private attributes as follows:

  private '_attributes' (CActiveRecord) => 
    array
      'contact_firstname' => string 'rec1' (length=4)
      'contact_lastname' => string 'rec1 lsname' (length=11)
      'contact_id' => string '1' (length=1)

<编辑:>

< >

我的标准,code的模型如下:

My criteria code in the model is as follows:

  $criteria->with = array(
    'owner', 
    'states', 
    'shopscontacts' => array(
      'alias' => 'shopscontacts',
      'select' => 'shopscontacts.contact_firstname,shopscontacts.contact_lastname',
      'together' => true
    )
  );

&LT; /编辑>

< / Edit >

我如何访问它们各自的列中的值?请帮忙! :(

How do I access the values in their respective columns? Please help! :(

推荐答案

嗯,我还没有使用的与()一起()方法了。有趣的是如何在列的价值的一部分, $数据 - &GT; shopscontacts 负载可达关系新鲜的基础上,的关系( )定义(而不是基于对标准你声明的)。

Hmm, I have not used the with() and together() methods much. What's interesting is how in the 'value' part of the column, $data->shopscontacts loads up the relation fresh, based on the relations() definition (and is not based on the criteria you declared).

一个更清洁的方法来处理阵列输出可能是这样的:

A cleaner way to handle the array output might be like this:

'value' => 'array_shift($data->shopscontacts)->contact_lastname'

也许是一个更好的办法来做到这一点,不过,将设立一个新的(附加)的关系,像这样在你的商店型号:

Perhaps a better way to do this, though, would be to set up a new (additional) relation, like this in your shops model:

public function relations()
{
  return array(
    'shopscontacts' => array(self::HAS_MANY, 'Shopsmodel', 'shop_id'), // original
    'firstShopscontact' => array(self::HAS_ONE, 'Shopsmodel', 'shop_id'), // the new relation
  );
}

然后,在你CGridView你可以设置一列像这样:

Then, in your CGridView you can just set up a column like so:

'columns'=>array(
  'firstShopscontact.contact_lastname',
),

干杯

这篇关于Yii的CGridview - 搜索/排序工作,但价值不被显示在各个小区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆