Treeview Instead of Menu

Hi,

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).

[sql]

CREATE SCHEMA "Policy";

CREATE TABLE "Policy"."Menus"

(

"MenuId" SERIAL NOT NULL PRIMARY KEY,


"MenuCode" varchar(12) NOT NULL,


"Url" varchar(50) NOT NULL,


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

);

CREATE TABLE "Policy"."MenuLocalized"

(

"MenuItemId" SERIAL NOT NULL PRIMARY KEY,


"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"

AS

SELECT

"Menus"."MenuId", 


"Menus"."MenuCode", 


"Menus"."Url", 


"MenuLocalized"."LanguageId", 


"MenuLocalized"."MenuText"

FROM

"Policy"."Menus", 


"Policy"."MenuLocalized"

WHERE

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

[/sql]

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




<?php

$this->widget(

	'CTreeView',

	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.

/Tommy

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

/Tommy

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

http://www.yiiframework.com/doc/api/1.1/CTreeView

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

<?php

$this->widget(

'CTreeView',


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?

/Tommy

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




<?php

$this->widget(

        'CTreeView',

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

);

?>



and the code in SiteController.php




public function actionAjaxFillTree()

{

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

		exit();

	}

	$parentId = 0;

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

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

	}

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

		"SELECT m1.id, m1.name AS text, m2.id IS NOT NULL AS hasChildren "

		. "FROM tree AS m1 LEFT JOIN tree AS m2 ON m1.id=m2.parent_id "

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

		. "GROUP BY m1.id ORDER BY m1.name ASC"

	);

	$children = $req->queryAll();

	echo str_replace(

		'"hasChildren":"0"',

		'"hasChildren":false',

		CTreeView::saveDataAsJson($children)

	);

	exit();

}



generates the following html.

[html]

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

</ul>

[/html]

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()

/Tommy