Would this require relational databases? CGridView Button Functionality

Hello everyone,

I’m trying to add the following functionality, however I’m not sure where to start. Any advice, examples, or direction would be greatly appreciated.

I have added a button to the cgridview of the main model in this context. Each of the records available in the cgridview for this model, have an attribute called lot (This table/model accounts for all the possible records).

There is another secondary table/model in my database that has records with this same lot attribute. However, this table only has certain records out of all the possible records and has other different attributes.

What I’d like to do is, using the lot attribute, search the secondary table to see if there is a corresponding row which contains that lot and more information. If so, I would like the button to be displayed in my CButtonColumn and link to the view of the secondary table. If not, I would like nothing to appear.

Thanks for any help. If any clarification is required, I would be happy to do so.

It sounds like you want to use a relation.

In your main table model, you’ll need a method that looks like this:

public function relations()


	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'secondTable' => array(self::HAS_ONE, 'SecondTableModel', 'lot_id'),



Then you should be able to access the related row from the first table like this:

$model = MainTable::model()->findByPk(4);

$lotModel = $model->secondTable;

Thanks for the help.

I’ve read up on relations however, I’m still very confused.

In my main model PackagingMetric I have the following code

public function relations()


return array(

   'bpr_errors' => array(self:HAS_MANY, 'BPRError', 'lot'),



in the secondary model, BPRError, that contains the corresponding matching rows by lot I have

public function relations()


return array(

 'lot' -> aray(self:BELONGS_TO, 'PackagingMetric', 'lot'),



Can anyone verify this would be the way to relate the two?

Also how can I use to check a relation exists for a particular data row in a view for instance, so I can link to the view for BPRError.

That looks correct, assuming your PackagingMetric model’s table has the id column, and your BPRError model’s table has the lot column.

HAS_MANY relations return an array, so you can check for results like this:


    do something...

BELONGS_TO relations return an object or null, so you can check them like this:

if($model->lot !== null)

    do something...


I think I am slowly starting to understand now.

I’ve tried your method for the visible attribute of one of my custom buttons on Cbuttoncolumn. I’d like for this button to only appear if that particular row has a BPRError associated with it.

I’ve tried this

'visible' => 'count($model->bpr_errors)'

(model undefined)

also this

'visible' => 'count($data->model->bpr_errors)'

neither seem to work and return respective errors. Any ideas on how to rectify this, or is my conditional clause not correct in this context?

Looking at the documentation for CButtonColumn, it looks like $data is the model itself.

Try this:

'visible' => 'count($data->bpr_errors)'


Ok tried that earlier as well, didn’t work at first. However, the issue was resolved in one of the relations functions by changing -> to =>

It doesn’t seem to be generating any errors, however no button seems to be appearing for any of the records, when it should for some.

To clarify, I have the attribute lot which is the identifier I’m using to match BPRerror’s for specific PackagingMetric’s. Both tables have an ID Column. They both contain the same lot column.

Any ideas?

Sounds like a problem with your relations.

I’d recommend setting up a test page, and just try something like this:

$model = PackagingMetric::model()->findByPk('some id that exists');

echo "".count($model->bpr_errors);

If the PackagingMetric model is showing zero error relations, then make sure your tables are setup properly, and that the relations exist.

Thanks will try!

One more thing. This may sound like a noob question, but I have never worked with relational data before. When you say set the tables up properly, is there a specific format column names or anything should be setup to accomodate this for Yii relations? I am using phpMyAdmin.

As long as your tables have a primary key, Yii’s CActiveRecord class is pretty good about figuring it out.

Your foreign key (in this case lot) should also be the same type as the primary key of the table it points to.