Skip to content

Instantly share code, notes, and snippets.

@violetyk
Created November 16, 2011 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save violetyk/1369984 to your computer and use it in GitHub Desktop.
Save violetyk/1369984 to your computer and use it in GitHub Desktop.
[cakephp]treeviewを作るサンプル1/3(モデル)
<?php
//モデル
function generateCategoryTree($id=0, $column_id='id', $column_name='name') {
$tree = array();
if($id == 0) {
$children = $this->find(
'all',
array(
'fields' => array($column_id, $column_name),
'conditions' => array(
'parent_id' => 0,
),
)
);
} else {
$children = $this->children($id, true, array($column_id, $column_name));
}
foreach($children as $child) {
$id = $child[$this->name][$column_id];
$name = $child[$this->name][$column_name];
if($this->childcount($id, true) > 0) {
$tree[$id] = array($name, $this->generateCategoryTree($id));
} else {
$tree[$id] = $name;
}
}
return $tree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment