About the method of Yii Framework to obtain all subclasses under the category

不言
Release: 2023-04-01 09:14:02
Original
1783 people have browsed it

This article mainly introduces the method of Yii Framework framework to obtain all subclasses under the classification. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Yii is a A component-based, high-performance PHP framework for developing large-scale web applications. Yii has almost all the features, including MVC, DAO/ActiveRecord, I18N/L10N, caching, JQuery-based AJAX support, user authentication and role-based access control, scaffolding, input validation, widgets, events, theming, and web services etc.

Get all subcategories under the category:

static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True)
 {
  static $arrTree; //使用static代替global
  if(!$all) $arrTree ='';
  if( empty($arrCat)) return FALSE;
  $level++;
  if($level == 1) $arrTree[] = $parent_id;
  foreach($arrCat as $key => $value)
  {
   if($value['parent_cid' ] == $parent_id)
   {
    //$value[ 'level'] = $level;
    $arrTree[] = $value['cid'];
    unset($arrCat[$key]); //注销当前节点数据,减少已无用的遍历
    self::getMenuTree($arrCat, $value[ 'cid'], $level);
   }
  }
  return $arrTree;
 }
Copy after login

The prerequisite for using the above method is to write the category into the cache file. The method of writing cache files is as follows:

public function actionIndex2()
 {
 $filepath = Yii::getPathOfAlias('application').'/data/';
 $arr = array();
 $db = Yii::app()->db;
 $listinfo = $db->createCommand("select name,cid,parent_cid,root_cid from item_cat_info")->queryAll();
 foreach($listinfo as $val)
 {
   $arr[$val['cid']] = array('cid'=>$val['cid'],'name'=>$val['name'],'parent_cid'=>$val['parent_cid'],'root_cid'=>$val['root_cid']);
 }

 $applist = "<?php\nreturn ".var_export($arr, true).";\n?>";
 file_put_contents($filepath.&#39;itemcat.php&#39;, $applist);
  }
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s learning. For more related content, please pay attention to PHP Chinese net!

Related recommendations:

How to use magic methods to implement cross-file calling functions through the Yii framework

Usage Yii2 rbac permission control menu menu

The above is the detailed content of About the method of Yii Framework to obtain all subclasses under the category. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!