[Solved] Category Table With Id And Parentid

I have a category table with an ID and a parentID in order to have sub-categories.

I would like to display the description of the sub-category in the CGridView.

I have the following code:

in the model




public $parentName;


public function rules()

{

...

   array('id, name, parentID, parentName, position, active, pageContent', 'safe', 'on'=>'search'),

...

}


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(

                ...

		'selfParent' => array(self::BELONGS_TO, 'Category', 'parentID'),

	);

}


public function search() {


                ...


		$criteria->together = true;

		$criteria->with = array('selfParent');

		

		if($this->parentName)

        {

                $criteria->compare('selfParent.name', $this->parentName, true);

        }

}



then in the view, i have the following code:




	array(

	    'name'=>'parentName',

            'value' => '$data->selfParent->name',

        ),



But this gives an error "Trying to get property of non-object "

Can somebody help me out of the woods?

Hi,

please see it

i hope it’s some help and also you can write a query on value field.

like


 'value'=>'Users::Model()->FindByPk($data->user_id)->username',

Thanks for the response!

Still stuck, though, on the following line:




'value' => 'Category::Model()->FindByPk($data->category->parentID)->name'



and the bit that is giving problems is


$data->category->parentID

because if I do this it works:




Category::Model()->FindByPk(2)->name



I have this declared in the 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(

			'projects' => array(self::HAS_MANY, 'Project', 'categoryID'),

			'category' => array(self::BELONGS_TO, 'Category', 'parentID'),

		);

	}



[size=2]i think you can try[/size]


$data->parentID

not in


$data->category->parentID

[size=2]Tried that but same problem :-([/size]




Trying to get property of non-object 

/var/www/yii-1.1.12/framework/base/CComponent.php(607) : eval()'d code(1)



Thanks for your time, appreciate it!

Try




	array(

	    'name'=>'parentName',

            'value' => '@$data->selfParent->name',

        ),



Hi.

You can do:




var_dump($model->attributes);



to check if "parentId" has a correct value.

If your model has a wrong “parentId”, you’ll see that error.

The value of "parentId" must exist in your parent table.

And this:




array(

            'name'=>'parentName',

            'value' => '$data->selfParent->name',

        ),



should work.

Regards.

feeling encouraged by your support, and will not give up until it works…

to be clear, here is the definition of the table in question:




CREATE TABLE IF NOT EXISTS `category` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(45) DEFAULT NULL,

  `parentID` int(11) DEFAULT NULL,

  `position` int(11) DEFAULT NULL,

  `active` tinyint(1) DEFAULT '1',

  `pageContent` text,

  PRIMARY KEY (`id`),

  UNIQUE KEY `name` (`name`),

  KEY `parentID` (`parentID`),

  KEY `position` (`position`),

  KEY `active` (`active`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;



then, in the model relations i had:




'parent_category' => array(self::BELONGS_TO, 'Category', 'parentID'),



which i have changed to:




'parent_category' => array(self::BELONGS_TO, 'Category', 'id'),



and in the view i have this:




array(

	'name'=>'subCategory',

	'value'=>'$data->parent_category->parentID',

	'htmlOptions'=>array('width'=>'100px'),

),



which gives the value of the parentID fine, but if i change the ‘value’ line to:




        'value'=>'Category::Model()->FindByPk($data->parent_category->parentID)->name',



then i get the following error:




Trying to get property of non-object 



:stuck_out_tongue:

But…I think you are confused.

You have :

  • "category" table.

  • "Category" model.

  • I guess you have a parent table, for example "parentCategory" (with its corresponding model "ParentCategory").

Then, you should have the following:

Model "Category"




'parent_category' => array(self::BELONGS_TO, 'ParentCategory', 'parentID'),

Category View




array(

        'name'=>'subCategory',

        'value'=>'$data->parent_category->parentName',

        'htmlOptions'=>array('width'=>'100px'),

),



And this should work.

And this is wrong:




Category::Model()->FindByPk($data->parent_category->parentID)->name



Because “parentID” isn’t Category’s primary key. It´s parentCategory’s primary key.




ParentCategory::Model()->FindByPk($data->parent_category->parentID)->name



And the right thing would be to use:




$data->parentID



Instead this:




$data->parent_category->parentID



Regards.

i show the solution here in case it is of help to someone with a similar scenario.

in the CGridView columns array, add the following entry:




array(

	'name'=>'parentID',

	'value'=>'$data->getParentCategory()',

),



in your model, add the function:




	public function getParentCategory() 

	{

		$res = Category::model()->findByPk($this->parentID);

		

		if ($res)

			return $res->name;

		else

			return '';

	}



thank you all for the support!

one last (newbie) question, how do i mark this as "SOLVED"?

Igastmans, did you read my last message????

It’s impossible for this to work properly:




Category::model()->findByPk($this->parentID)



Because “parentID” isn’t a primary key!!!!!! And you’re using “findByPk”!!!!!

It works by chance when "parentID" = "id". Otherwise it will return an empty string.

I mean, it works because you’re controlling the error.

If you write :




public function getParentCategory() 

{

   $res = Category::model()->findByPk($this->parentID);


   return $res->name;

}



It will still fail.

Furthermore, this:




$data->getParentCategory()



Should be:




$data->ParentCategory



I don’t understand how it can work for you.

To mark a post as "solved", edit your first message and type [SOLVED] at the beginning of the title.

Regards.

Dear lagogz,

Somehow your previous post did escape me… don’t know how that happened.

Sorry, I might not have been clear in my initial post that I am dealing with only one table, which has and ‘ID’ field as primary key, and a ‘parentID’ field, which if blank => parent category, and not blank => subcategory.

So I guess that from the start I was wrong trying to make it work by setting the following relation, because, as you point out in your last message, ‘parentID’ is not the Primary Key.




'parent_category' => array(self::BELONGS_TO, 'Category', 'parentID'),



Many thanks for responding, I have learnt quite a few things, amongst them to take a break from my keyboard in time…

Know that I was freaking out!!!!!!!!!!!!

Alright then although I think you should make 2 tables, Category and SubCategory for this scenario and to use relationships.

Actually, you don’t need the relationship as you have raised.

Regards.

Sorry about that :slight_smile: