php - Yii Framework | Nested arrays in same table -



php - Yii Framework | Nested arrays in same table -

using cactiverecord table looks this:

a column parent_id has relation many id, , works properly.

id | parent_id ---+---------- 1 1 <- top 2 1 <- means parent_id 1 has parent id=1 3 1 4 2 <- parent id=2 5 2 6 2 , many nested levels....

a goal how nested php classically way nested arrays info (arrays within arrays). array(1,1) { array(2,1) { array(4,2) .... } }

problem yii. didn't find way how pick info nested array using cactiverecord.

what best way create nested array results? main goal easy forwards render view don't separate many functions , calling many models outside modules or models.

a 1 function result.

solved using this: recursive function generate multidimensional array database result

you need info arrays model:

$somemodel = mymodel::model()->findall();

then set in array rather yii objects model or need:

foreach ($somemodel $k => $v) { $arrays[$k] = array('id' => $v->id, 'parent_id' => $v->parent_id, 'somedata' => 'your data'); }

then phone call function:

function buildtree(array $elements, $parentid = 0) { $branch = array(); foreach ($elements $element) { if ($element['parent_id'] == $parentid) { $children = buildtree($elements, $element['id']); if ($children) { $element['children'] = $children; } $branch[] = $element; } } homecoming $branch; }

then phone call set $arrays info buildtree function rebuild nesting arrays data.

$tree = buildtree($arrays);

now $tree nested arrays data.

note: there aren't depth function in convient way can add together using sample: create nested list multidimensional array

php arrays yii frameworks

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -