Hello,
I’m trying to display a value from a foreign table in my CGridView. I followed example in other posts but I can’t get it to work. Can you spot where I’m making my error?
I’m trying to get the projectname string to display. I tried both methods that I found in other posts:
project.projectname
$data->project->projectname
CGRIDVIEW
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'timeentries-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'filterPosition'=>'body',
'columns'=>array(
'project',
array('name'=>'project','value'=>'$data->project->projectname'),
'project.projectname',
'projectitem',
'date',
'notes',
array(
'class'=>'CLinkColumn',
'header'=>'Time',
'labelExpression'=>'$data->time',
'cssClassExpression'=>'$data->internalid',
//'htmlOptions'=>array('id'=>'$data->internalid'),
'linkHtmlOptions' => array('class'=>'fly')
),
array(
'class'=>'CButtonColumn',
),
),
));
RELATION SET IN MY MODEL
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(
'project'=>array(self::BELONGS_TO, 'Projects', 'project'),
);
}
CREATE TABLE VIEW IN DATABASE
CREATE TABLE `timeentries` (
`time` time DEFAULT NULL,
`date` date NOT NULL,
`beg_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`end_timestamp` timestamp NULL DEFAULT NULL,
`project` int(10) unsigned NOT NULL,
`internalid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`notes` varchar(400) DEFAULT NULL,
`projectitem` int(10) unsigned DEFAULT NULL,
`timerunning` tinyint(4) DEFAULT NULL,
`createdby` int(10) unsigned NOT NULL,
`createddate` datetime NOT NULL,
`updatedby` int(10) unsigned DEFAULT NULL,
`updateddate` datetime DEFAULT NULL,
`accountnumber` int(10) unsigned NOT NULL,
PRIMARY KEY (`internalid`),
KEY `projectitem` (`projectitem`),
KEY `project` (`project`),
KEY `createdby` (`createdby`),
KEY `updatedby` (`updatedby`),
KEY `accountnumber` (`accountnumber`),
CONSTRAINT `timeentries_ibfk_1` FOREIGN KEY (`projectitem`) REFERENCES `projectitem` (`internalid`) ON UPDATE CASCADE,
CONSTRAINT `timeentries_ibfk_2` FOREIGN KEY (`project`) REFERENCES `tbl_projects` (`internalid`) ON UPDATE CASCADE,
CONSTRAINT `timeentries_ibfk_3` FOREIGN KEY (`createdby`) REFERENCES `users` (`internalid`) ON UPDATE CASCADE,
CONSTRAINT `timeentries_ibfk_4` FOREIGN KEY (`updatedby`) REFERENCES `users` (`internalid`) ON UPDATE CASCADE,
CONSTRAINT `timeentries_ibfk_5` FOREIGN KEY (`accountnumber`) REFERENCES `account` (`internalid`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1