Treeview Instead of Menu


I am very new to yii. I also do have a limited understanding of MVC approach.

I created a new application named "My Application". I have already been playing with the framework and decided to change the layout to a different one. I connected to a database and created some models and CRUD. All went fine. The main.php in views\layouts\ was very intelligent enough to explain novice users like me that yii uses blueprint css framework. So, it was very easy for me to change the layout accordingly.

I changed the template to put the menu on the left hand side and the rest on right side, and it worked absolutely fine. I wanted to replace inbuilt menu with treeview which would be

role based,

database driven,

environment aware (at the beginning en-US)

For that, I created new tables in database (PostgreSQL).



CREATE TABLE "Policy"."Menus"



"MenuCode" varchar(12) NOT NULL,

"Url" varchar(50) NOT NULL,

"ParentMenuId" integer NULL REFERENCES "Policy"."Menus"("MenuId")


CREATE TABLE "Policy"."MenuLocalized"



"MenuId" integer NOT NULL REFERENCES "Policy"."Menus"("MenuId"),

"LanguageId" integer NOT NULL, --REFERENCES "Core"."Languages"("LanguageId"),

"MenuText" varchar(50) NOT NULL,

CONSTRAINT "Policy_MenuLocalized_uix" UNIQUE("MenuId","LanguageId")


CREATE VIEW "Policy"."MenuView"












"Menus"."MenuId" = "MenuLocalized"."MenuId";


I changed the lines of main.php (in menu placeholder) to




	array('url' => array('ajaxFillTree'))



Now, I do not know what to do next. I was unable to find the controller of main.php with many failed guesses. Please help.

Since you’ve generated crud code there will already be a base controller Controller.php generated for you in the protected/components folder. The other controllers will inherit from this one.


I have SiteController.php, there is no Controller.php.

If you used Gii for generating crud for your models, it should be there. Otherwise just create a base controller, have it inherit from CController and your other controllers inheriting from this base controller.

Here’s a link to the section about controllers in The Definitive Guide to Yii


I want to replace menu with treeview, not on individual views, but on layout itself. According to the example found on this site.

I added these lines to protected/views/layouts/main.php




array('url' =&gt; array('ajaxFillTree'))



Now, where should I place actionAjaxFillTree() ?

I already tried to paste the function in SiteController.php but that does not work. Hope I am clear this time.

I understand what you want to do. I was wrong about just using the base controller with no other changes. An accessrule for ajaxFillTree has to be merged or repeated (in the other controllers).

If you use controller/action in the url it should work to call any controller and in fact what you already tried should work for the home page (which uses SiteController).

What is the actual problem with the treeview? Did you check the request and response in Firebug?


Since I am a new user, I was not allowed to have more than 3 post in one day which is quite annoying.

The code




        array('url' => array('ajaxFillTree'))



and the code in SiteController.php

public function actionAjaxFillTree()


	if (!Yii::app()->request->isAjaxRequest) {



	$parentId = 0;

	if (isset($_GET['root'])) {

		$parentId = (int) $_GET['root'];


	$req = Yii::app()->db->createCommand(

		"SELECT, AS text, IS NOT NULL AS hasChildren "

		. "FROM tree AS m1 LEFT JOIN tree AS m2 ON "

		. "WHERE m1.parent_id <=> $parentId "



	$children = $req->queryAll();

	echo str_replace(







generates the following html.


<ul class="treeview" id="yw0">



BTW, the query used in the command "createCommand" of "actionAjaxFillTree" is not according to the tables I mentioned earlier. Purposefully expecting yii to throw errors and then adjust accordingly.


Can anybody help?

You should split this problem into two parts:

  1. Create a static array and return it to the view by echoing the result from CTreeView::saveDataAsJson().

  2. Fix the query. It seems like your query returns an empty result. (API reference)

BTW it’s recommended you use Yii::app()->end() instead of exit()