Yii Framework Forum: Making CGridView Display Different Values From Different Models - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Making CGridView Display Different Values From Different Models Rate Topic: -----

#1 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 07:54 PM

I have a model of a particular table which is running fine and works great with CGridView. The main issue I have is that I need to display information from other models based on values from the main model.

For example, lets say I have a "main model" called "Customers." They have some kind of field that indicates a kind of ice cream they got with a number. So, in the model -

class Customer extends CActiveRecord {
    ...
    private $iceCreamType; //stores a number (0/1/2) to determine the column to look at
    ...
}


In another model, "IceCream", it defines what each kind of ice cream is:

class IceCream extends CActiveRecord {
    ...
    private $iceCreamType-1;  //Chocolate
    private $iceCreamType-2;  //Vanilla
    private $iceCreamType-3;  //Strawberry
    ...
}


In this case, CGridView is using "Customer" as its main model as that's where most of the data comes from. My question is, how do I tell CGridView to use Customer::iceCreamType in the data set from the IceCream model to obtain its output value? I guess I'm looking for something like this:

<?php $this->widget( 'zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
        array( 
            'name' => 'iceCreamType',
            'header' => 'Type of Ice Cream',
            'value' => '$iceCreamData[ "iceCreamType-" . $data->iceCreamType ]',
        ),
        ...
    ),
);


Are there any ideas on this? I tried something like this to no avail:

'value' => 'IceCream::model()->findBySQL( "SELECT iceCreamType-" . $data->iceCreamType . " FROM IceCream WHERE ...'" ),


In addition to not working properly, it looks very ugly and isn't super usable. Any ideas? Thanks!
0

#2 User is offline   jsoo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 01-March 10
  • Location:Durham, NC, USA

Posted 15 March 2010 - 08:51 PM

I realize your example is an illustration, but it doesn't make sense for IceCream to have three attributes to define three types, as though they were constants. One attribute ought to do it.

At any rate, you need to define a relation in each model that reflects this. Then you can call on these relations in constructing your data provider.

Customer will have:

public function relations()
{
    return array(
        'iceCream'=>array(self::BELONGS_TO,'IceCream','iceCreamType'),
    );
}


IceCream will have:

public function relations()
{
    return array(
        'customers'=>array(self::HAS_MANY,'Customer','type'), // if you rename IceCream's attribute to 'type'
    );
}


And the data provider:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>new CActiveDataProvider('Customer',array(
        'criteria'=>array(
            'with'=>array( 
                'iceCream'=>array(
                    'select'= 'type',
                ),
            ),
        ),
    )),
));

0

#3 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 08:54 PM

While I know my example seems completely wrong due to the fact that the table design is not very great, you don't always get to choose what you're working with and you just have to run with it :)

Thanks a bunch for the example. That looks like it'll work out, or at least I'll be able to give it a shot!
0

#4 User is offline   jsoo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 01-March 10
  • Location:Durham, NC, USA

Posted 15 March 2010 - 08:58 PM

View PostDoug Swain, on 15 March 2010 - 08:54 PM, said:

While I know my example seems completely wrong due to the fact that the table design is not very great, you don't always get to choose what you're working with and you just have to run with it :)


Understood! In that case you'll need three separate relations each way. Ughh, what if they go to Baskin-Robbins and you have to add 54 more attributes? (I suppose I'm showing my age with that one.)

More info.
0

#5 User is offline   Doug Swain 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 28-December 09

Posted 15 March 2010 - 09:05 PM

Hahah! That's hilarious. And frankly, very true... eek.

I think, though, it might be a fixed number of "types" of "ice cream" in my actual case (the Ice Cream analogy is a little flawed in this way) but that doesn't make it anymore logical really... especially when you're trying to do things in a clean fashion.

Thanks for the link! The relations never really made much sense to me from working through the blog tutorial or reading things in the API but this looks like it'll clear stuff up. Awesome.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users