Yii CGridview - search/sort works, but values aren't being displayed on respective cells

淺唱寂寞╮ 提交于 2019-12-11 05:40:14

问题


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

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');

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.

On CGridview file, I'm doing something like

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

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)'
    ),

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)

< Edit: >

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
    )
  );

< / Edit >

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


回答1:


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
  );
}

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

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

Cheers




回答2:


Since 'shopscontact' is the name of the has-many relation, $data->shopscontact should be returning an array with all the shops related... did you modify the relation in order to return only one record (if I didn't get you wrong, you only need to display one, right?)? If you did it, may I see your filtering code?

P.S. A hunch to get a fast but temporal solution: have you tried 'value' => '$data->shopscontact['contact_firstname']'?



来源:https://stackoverflow.com/questions/8857314/yii-cgridview-search-sort-works-but-values-arent-being-displayed-on-respecti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!