Use Of Empty Parent_Id


I’ve got a db table with all categories and subcategories. They are connected with a column parent_id.

Some people say that using an empty value “” for the top level categories is better than using a zero “0” value. It’s true that this is handy when is comes to dropdown lists and you can use the empty option.

But now I got my admin page and I want to use draggable sorting. It’s too complex for me to build it in one page like this wiki suggests (

So I thought to only show the main categories and when I click on one I show the child categories. So I can use the UI sortable function on that which works great.

But how to start with only the main categories, an empty parent_id just gives me all the categories.

	public function actionAdmin()


		$model=new Category('search');

		$model->unsetAttributes();  // clear any default values		











Technically, only existing and NULL values are allowed for foreign keys.

Ok, I’ve changed “” into NULL in the table.




			// or like this, same result.

			$model->attributes=array('parent_id'=>new CDbExpression('NULL'));

Now gives me no results at all…

I’ve changed it into:

        // In actionAdmin:



        // In the model:

        public function scopes()


                return array(


                                'condition'=>'parent_id IS NULL',





That seemt to work correct. Not sure if this is the best solution though.