Custom form elements in Yii views

Hi, I am new to Yii and I am trying to implement the following:

I am having a dropdown list that holds a hierarchical tree (using id/parentid and type=0 [navigation links]), what I want is to do a recursion on the dropdownlist elements so that it will display them in that order:




ROOT

1. ITEM 1

1.1 ITEM 1.1

1.2 ITEM 1.2

1.2.1 ITEM 1.2.1

1.3 ITEM 1.3

2. ITEM 2

2.1 ITEM 2



etc,

In classic php I am using a recursion to achieve that (most of my work is in plain hierarchy like this).

Also using the same hierarchical data, is it possible to make a custom form element, for example a div block that has overflow:auto to look like a multiple dropdownlist (only visually) but instead of actually choosing on select, to rended a checkbox or a radio next to it.

All of the above are implemented in plain php, I am only trying to find ways to do them in yii (using widgets? components? zii extensions? )

So, I was able create a widget that would emulate the hierarchy(sort of), just like I am making them with the traditional PHP.

Now I have another problem which probably lies in the relationships / criteria / parameters.

I have defined this on my Posts model, relations function:




'childs' => array(self::HAS_MANY,'Post','parentId'),



Now as I mentioned earlier I have a type field, which is: 0 for navlinks, 1 for post titles and 2 for the post body itself.

In my Posts model I also have this:




	public function getChildren(array &$out,$level=0)

	{

		array_push($out,array(

			"level"=>$level,

			"id"=>$this->id,

			"title"=>$this->content,

			'isActive'=>$this->isActive,

			"isType"=>$this->isType)

		);

		if ($this->childs)

		{

			foreach($this->childs as $child)

				$child->getChildren(&$out, $level+1);

		}

	}



With the code above, I get all my hierarchy as a 2d array (1d is the index, 2d are the fields), so that I could feed it to CHtml::listData.

I am using this as a criteria:




			$criteria = new CDbCriteria();

			$criteria->condition = 't.parentId=:parent AND t.isType=0';

			$criteria->params = array(

				':parent' => 0,

			);


$data = Post::model()->findAll($criteria);

$items = $data->getChildren(0);



when I am using the above, I get ALL the data (isType is not being used on the childs relation). So I get the main categories, but other than that, I get every other unfiltered field (like a select * from … with parentid=0, recursively). If i use isType=1 or 2 I get nothing filtered.

So what am I doing wrong here?

if you want to do some filtering on the a table related to the one your actually doing the query on you’ll have to use the correct prefix on the criteria condition.ciao

t refers to the "parent" table

to refer to the "child" table you have to use the prefix you choosed in your relation, which in this case is childs

How about on both parent/child tables? Since my initial parentid is 0, I am recursively doing a filter where parentid changes but type remains the same.

Typically in SQL I would write:




Select id FROM Posts where parentid=0 and type=0

/*loop 1 start, for example let's say id has values 1,2,3*/

- Select id FROM Posts where parentid=1 and type=0 (Child #1)

/* recursion loop, values 4,5,6 */

-- Select id FROM Posts WHERE parentid=4 and type=0

/* etc */

- Select id FROM Posts where parentid=2 and type=0 (Child #2)

/* recursion loop */

- Select id FROM Posts where parentid=3 and type=0 (Child #3)



I am not using nested set example (or the appropriate plugins) since my data are already prefixed from previous versions (non yii).

If I use the declaration on relations[]




'childs' => array(self::HAS_MANY,'Post','parentId'),



and use the following for fetching my parentid 0 records:




$criteria = new CDbCriteria;

[..] fetch id, parentid, type and title

$criteria->condition = 't.parentId=0 AND t.type=0';

$record = Posts::model()->findAll($criteria);

print_r($record->childs);



It brings me correctly the filtered type 0 parent records, but the childs have type 0,1,2 as well!

That is what I am trying to achieve, filter the childs as well.