Difference between #11 and #12 of
Display an AJAX tree from your DB using CTreeView

Revision #12 has been created by François Gannaz on May 25, 2012, 11:36:53 AM with the memo:

Note on debugging AJAX, removed `exit()` by default
« previous (#11)


Title unchanged

Display an AJAX tree from your DB using CTreeView

Category unchanged


Yii version unchanged

Tags unchanged

AJAX, CTreeView

Content changed

* Should receive parent node ID in $_GET['root'],
* with 'source' when there is no parent.
public function actionAjaxFillTree()
// accept only AJAX request (comment this when debugging)
if (!Yii::app()->request->isAjaxRequest) { exit(); } // parse the user input
$parentId = "NULL"; if (isset($_GET['root']) && $_GET['root'] !== 'source') { $parentId = (int) $_GET['root']; } $// read the data (this could be in a model)
reqn = 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
echo str_replace(
When sending the JSON-encoded result, there's a little trick: the javascript wants the `hasChildren` attribute to be a boolean. But the SQL result just contains 0 or 1, so we have to convert it, at least for the "false" case. Instead of operating on the string result, one could modify the PHP array with a `foreach`.

The `exit()` might not be necessary, but if you enabled the logs to Firebug or extensions like a debug toolbar, then Yii may write some JS at the end of your response that would break the JSON format. The drawback of### If AJAX fails
If your AJAX query fails, you should try to debug it in your browser. Firefox, Chrome and Opera have good developer toolbars that will show you the AJAX requests sent and what their answers were. You may find that some HTML is appended at the end of the expected JSON answer. I had this problem with Yii'JS logs and a toolbar extension. In this case, insert an
`exit();` isat that logs won't be written, whereas `Yii::app()->ende end of the action. Or even better: `Yii::logger->flush()`; ends gracefullyxit();`.

### Final note
This is meant as a simple introduction. There are many ways to enhance this:

* Refactor the
controller action by putting its 32 last lines into a model. * Inscrease the abstraction level, so that the code can be used on various models, as long as they share the same table structure. * If the tree content rarely changes, you may consider using a different structure, like nested sets. This will make most queries easier, e.g. fetching the path of a node.
19 0
Viewed: 64 245 times
Version: 1.1
Category: Tutorials
Written by: François Gannaz
Last updated by: François Gannaz
Created on: Apr 7, 2010
Last updated: 8 years ago
Update Article


View all history