Use the database-controlled CMenu menu

Source: Internet
Author: User
What is the database structure of the CMenu menu controlled by the database?
1234567891011121314151617181920212223242526272829 Create table if not exists 'menu '('menu _ id' int (11) not null auto_increment, 'name' varchar (255) not null, 'date _ added 'datetime not null, 'Last _ updated' datetime not null, 'status' enum ('active', 'inactive') not null, primary key ('menu _ id'), unique key 'name _ UNIQUE '('name') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 1; create table if not exists 'menu _ item' ('item _ id' int (11) not null auto_increment, 'parent _ id' int (11) defaultNULL, 'menu _ id' int (11) not null, 'label 'varchar (255) not null, 'URL' text not null, 'description' text not null, 'Date _ added' datetime not null, 'Last _ updated' datetime not null, 'sort _ order' int (11) not null, 'status' enum ('active ', 'Inactivity') not null, primary key ('item _ id'), KEY 'fk _ menu_item_menu1 '('menu _ id '), KEY 'fk _ menu_item_menu_item1 '('parent _ id') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 1; alter table 'menu _ item' add constraint 'fk _ menu_item_menu1 'foreign key ('menu _ id') REFERENCES 'menu' ('menu _ id') ondeletecascade on update no action, add constraint 'fk _ menu_item_menu_item1 'foreign key ('parent _ id') REFERENCES 'menu _ item' ('item _ id') ondeleteset null on update no action;
In the model, how does one retrieve menu items?
123456789101112131415161718192021222324252627282930313233 PublicfunctiongetItems ($ menu_id, $ parent_id = null) {$ results = Yii: app ()-> getDb ()-> createCommand (); $ results-> select ('item _ id, label, URL')-> from ('{menu_item}'); if ($ parent_id = null) $ results-> where ('menu _ id =: mid AND parent_id IS Null', array (': mid' => (int) $ menu_id )); else $ results-> where ('menu _ id =: mid AND parent_id =: pid ', array (': mid '=> (int) $ menu_id ,': pid '=> $ parent_id); $ results-> order ('sort _ order ASC, label ASC'); $ results = $ results-> queryAll (); $ items = array (); if (empty ($ results) return $ items; foreach ($ resultsAS $ result) {$ childItems = $ this-> getItems ($ menu_id, $ result ['item _ id']); $ items [] = array ('label' => $ result ['label'], 'URL' => $ result ['URL'], 'itemopexception' => array ('class' => 'listitem '), 'linkoptions' => array ('class' => 'listitemlink', 'title' => $ result ['label']), 'submenuoptions' => array (), 'items '=> $ childItems,);} return $ items ;}
CMenu initialization?
12345678910 // Get the menu with id #2 $ items = $ this-> getItems (2); $ menu = array ('id' => 'Nav ', 'activecssclass '=> 'selected', 'linklabelwrapper' => null, 'htmloptions' => array ('class' => 'topnav '), 'items '=> $ items); $ this-> widget ('zii. widgets. CMenu ', $ menu );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.