AhmetS
(Ahmet)
1
I have a foreign key in my product table which is optional.
I have used the crud generator for the product table, but as you will be aware, I am now faced with turning id’s into names.
I am faced with an error caused by my customised view.php when trying to call getAttributeLabel on a non-object.
The problem is that the country field is nullable field. If the country is not set (i.e. null), i get the error.
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'product_type_id',
array(
'label' => $model->country->getAttributeLabel('country'),
'value' => $model->country->country
),
'name',
),
)); ?>
Any recommendations on how to fix this?
Keith
(Kburton)
2
Does this not work for you?
'attributes'=>array(
'id',
'product_type_id',
'country.country',
'name',
),
AhmetS
(Ahmet)
3
It does work for me! I didn’t know you could do that!
Thank you!!!
AhmetS
(Ahmet)
4
Final question related to same topic, but to the scaffolded _view.php partial view.
I have modified my ProductController.php, actionIndex method as follows to:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'with'=>array('productType', 'country'),
),
'pagination'=>array(
'pageSize'=>20,
),
));
now, the problem is that country is a nullable field.
As such, when I amend the _view.php as follows to display the country name:
<b><?php echo CHtml::encode($data->getAttributeLabel('country_id')); ?>:</b>
<?php echo CHtml::encode($data->country->country); ?>
<br />
I get a PHP notice, trying to get a property of non-object.
Keith
(Kburton)
5
Either
<? if ($data->country): ?>
<b><?php echo CHtml::encode($data->getAttributeLabel('country_id')); ?>:</b>
<?php echo CHtml::encode($data->country->country); ?>
<br />
<? endif; ?>
or
<b><?php echo CHtml::encode($data->getAttributeLabel('country_id')); ?>:</b>
<?php echo CHtml::encode($data->country ? $data->country->country : 'None'); ?>
<br />