Yii Framework Forum: Optional Foreign Key - Call To A Member Function Getattributelabel() On A Non-Object... - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Optional Foreign Key - Call To A Member Function Getattributelabel() On A Non-Object... Rate Topic: -----

#1 User is offline   AhmetS 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 01-May 13

Posted 01 May 2013 - 06:33 AM

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?
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 01 May 2013 - 06:38 AM

Does this not work for you?

        'attributes'=>array(
                'id',
                'product_type_id',
                'country.country',
                'name',
        ),

0

#3 User is offline   AhmetS 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 01-May 13

Posted 01 May 2013 - 06:43 AM

It does work for me! I didn't know you could do that! :) Thank you!!!

View PostKeith, on 01 May 2013 - 06:38 AM, said:

Does this not work for you?

        'attributes'=>array(
                'id',
                'product_type_id',
                'country.country',
                'name',
        ),


0

#4 User is offline   AhmetS 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 01-May 13

Posted 01 May 2013 - 07:50 AM

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.
0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 01 May 2013 - 08:26 AM

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 />

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users