Yii Framework Forum: Self Referencing Active Record - Yii Framework Forum

Jump to content

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

Self Referencing Active Record Rate Topic: -----

#1 User is offline   montegobay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 13-November 11

Posted 16 May 2013 - 03:50 PM

My database has a table called 'tag' that references itself. When I try to use $model->parent->name I get the error "Trying to get property of non-object". Is there a problem with the way my relationships are defined? Any other ideas?

Here is my table setup:

tag_id
parent_id -->references tag_id
name

My Tags model has the following relationships defined:
	public function relations()
	{
		return array(
                        'parent' => array(self::BELONGS_TO, 'Tag', 'parent_id'), 
                        'children' => array(self::HAS_MANY, 'Tag', 'parent_id'), 
            
		);
	}



In my index file I am trying to loop through all tags to show their parent name using:
$data->parent->name;


That code yields the error "Trying to get property of non-object"


Here is my actionIndex:

	public function actionIndex()
	{
                $dataProvider=new CActiveDataProvider('Tag');
              
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

Here is my index code:

 $this->widget('bootstrap.widgets.TbListView',array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
)); 


Here is my _view code:
<div class="view">
	<b><?php echo CHtml::encode($data->getAttributeLabel('tag_id')); ?>:</b>
	<?php echo CHtml::link(CHtml::encode($data->tag_id),array('view','id'=>$data->tag_id)); ?>
	<br />
	<b><?php echo CHtml::encode($data->getAttributeLabel('parent_id')); ?>:</b>
	<?php echo $data->parent->name; ?>
	<br />
	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
	<?php echo CHtml::encode($data->name); ?>
	<br />
</div>

0

#2 User is offline   lucifurious 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 184
  • Joined: 15-March 09
  • Location:Atlanta, GA.

Posted 16 May 2013 - 07:47 PM

Are you sure your referential ids exist? Are there parent and children? If there my not be, you need to check validity before you use an object. That just good programming.

If ( $data->parent ) doSomethingCool( $data->parent->name ) ;
0

#3 User is offline   sefburhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 137
  • Joined: 08-February 12
  • Location:islamabad

Posted 17 May 2013 - 12:16 AM

View Postmontegobay, on 16 May 2013 - 03:50 PM, said:

My database has a table called 'tag' that references itself. When I try to use $model->parent->name I get the error "Trying to get property of non-object". Is there a problem with the way my relationships are defined? Any other ideas?

Here is my table setup:

tag_id
parent_id -->references tag_id
name

My Tags model has the following relationships defined:
	public function relations()
	{
		return array(
                        'parent' => array(self::BELONGS_TO, 'Tag', 'parent_id'), 
                        'children' => array(self::HAS_MANY, 'Tag', 'parent_id'), 
            
		);
	}



In my index file I am trying to loop through all tags to show their parent name using:
$data->parent->name;


That code yields the error "Trying to get property of non-object"


Here is my actionIndex:

	public function actionIndex()
	{
                $dataProvider=new CActiveDataProvider('Tag');
              
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

Here is my index code:

 $this->widget('bootstrap.widgets.TbListView',array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
)); 


Here is my _view code:
<div class="view">
	<b><?php echo CHtml::encode($data->getAttributeLabel('tag_id')); ?>:</b>
	<?php echo CHtml::link(CHtml::encode($data->tag_id),array('view','id'=>$data->tag_id)); ?>
	<br />
	<b><?php echo CHtml::encode($data->getAttributeLabel('parent_id')); ?>:</b>
	<?php echo $data->parent->name; ?>
	<br />
	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>
	<?php echo CHtml::encode($data->name); ?>
	<br />
</div>



Please go through this http://www.yiiframew...-relation-type/

and
http://www.yiiframew...en/database.arr
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