beentold Posted November 13, 2013 Share Posted November 13, 2013 Hello everyone! First of all, Larry, thank you for the Yii tutorial!!!I have just gone through it and I ran into a little bit of a dead end. In the Employee controller (line 126) it says: $dataProvider=new CActiveDataProvider('Employee'); I would like to add something like this: SELECT name FROM department WHERE id = employee.departmentId And then pass the department.name to the view instead of the employee.departmentId. I'm sorry if this is total BS. I'm a noob in MVC (and PHP/MySQL too, to be honest). I'm sure I could just find out how to write a proper MySQL query and pass it on in an array, but I think there must be a "simpler" (or more elegant) way to do this. Thanks in advance for any replies! Link to comment Share on other sites More sharing options...
Larry Posted November 16, 2013 Share Posted November 16, 2013 Thanks for the nice words. Just to confirm, you're trying to change how data is provided to a widget (like CGridView)? Link to comment Share on other sites More sharing options...
beentold Posted November 17, 2013 Author Share Posted November 17, 2013 Hello Larry. Thanks for taking the time to reply to my post! Thanks for the nice words. Just to confirm, you're trying to change how data is provided to a widget (like CGridView)? Yes, I think that's it. Basically, the way I understand it, CActiveDataProvider in EmployeeController.php on line 126 gets the employee table as a whole and then passes it on to the index view. $dataProvider=new CActiveDataProvider('Employee'); $this->render('index',array( 'dataProvider'=>$dataProvider, )); Which in turn passes it on to the _view widget. <?php $this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view', )); ?> In _view.php the list of employees is generated. The information from $dataProvider is now in $data. And on lines 12 and 13 the departmentId is displayed. <b><?php echo CHtml::encode($data->getAttributeLabel('departmentId')); ?>:</b> <?php echo CHtml::encode($data->departmentId); ?> I would like to lookup the department name and display that instead.Is there a way I can manipulate $dataProvider in the controller to include the departmentName (from the table department) instead of departmentId (from the table employee) - or in addition to it? Link to comment Share on other sites More sharing options...
Recommended Posts