Revision #7 was created by wei on Oct 26, 2010, 3:50:03 AM.
formatting, tags
Tags
menu
Content
[...]
When this commands run without an error, we should add two relation rules to our new created model:
```php
public function relations() {
return array(
'getparent' => array(self::BELONGS_TO, 'Hierarchy', 'parent'),
'childs' => array(self::HAS_MANY, 'Hierarchy', 'parent', 'order' => 'sort ASC'),
);
}
```
This reads as: the 'parent' of a row belongs to the parent-column of the same table, while we can gather the childs of a row by a relation of ourself with the `HAS_MANY` relation. We always want our childs get ordered by the column 'sort's. You can add additional relations if you want.
To test our newly created model, we need to insert some random test data:
}
```
We place this function in `models/Hierarchy.php`
This is a recursive function (note how the function calls itself) that gathers all subchilds of an element that are available
[...]
'items'=>$items,
));
```
to render the menu with the content of our database.
[...]
After this, we want our Users to be able to easily move Menu entries around. To achieve this, we will use a Drop-Down List,
in which we can choose the parent of our selected element. We write this code-snippet to `views/Hierarchy/_form.php`:
With this lines we will be able to create a "add entry to this element"-Button like this:
[...]
```php
if(!Yii::app()->User->isguest) echo CHtml::link("Add a new element", array('Hierarchy/create', 'hierarchyParent' => $model->id));
```
Place this lines somewhere at `views/Hierarchy/view.php`.
I hope my small tutorial was helpful for you. There are some points that can be made even better, for example someone could change the admin CGridView to be collapsable, and the elements could be moved around by drag & drop. In the next version of this Tutorial i will use the nestedset extension to achieve the Hierarchy Structure. Thank you for reading & trying, and
don't hesitate to ask me when you have Questions.