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.
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:
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?
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.
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.