Self-written thinkphp automatically generated classes

Source: Internet
Author: User

CqhModel.class.php

<?phpnamespace cqh\model;use think\model;class Cqhmodel extends model{protected $trueTabelName;      The table name in the corresponding database is protected $moduleName;  The corresponding module name protected $tpName;    The table corresponding TP name protected $fields;  All field information protected $tableComment;        Table notes Public Function Iniset ($tableName, $moduleName) {$this->builddir ($moduleName);        $this->setattribute ($tableName, $moduleName);    return TRUE;  }/*********************** build controller ***********************/Public Function Gcontroller () {$cDir = App_path . $this->modulename.        '/controller '; $file = $cDir. ‘/‘ . $this->tpname.        ' Controller.class.php ';        Ob_start (); Include (App_path.        ' cqh/template/controller.php ');        $str = Ob_get_clean ();        if (!is_file ($file)) {file_put_contents ($file, "<?php\r\n". $str);    } return TRUE;        }/*********************** Generate model ***********************/public Function Gmodel () {$mDir = App_path. $this->modulename. '        /model ';        if (!is_dir ($mDir)) {mkdir ($mDir, 0755,true); } $file = $mDir. ‘/‘ . $this->tpname.        ' Model.class.php ';        Ob_start (); Include (App_path.        ' cqh/template/model.php ');        $str = Ob_get_clean ();        if (!is_file ($file)) {file_put_contents ($file, "<?php\r\n". $str);    } return TRUE; /*********************** generates a static page ***********************/public Function Gview () {$vDir = App_path. $th Is->modulename.        '/view/'. $this->tpname;        if (!is_dir ($vDir)) {mkdir ($vDir, 755,true);        } $tableComment = $this->tablecomment;        $arr = Array (' Add ', ' save ', ' lst '); foreach ($arr as $v) {$file = $vDir. "            /$v. html ";            Ob_start (); Include (App_path.            "cqh/template/$v. html");            $str = Ob_get_clean (); if (!is_file ($file)) {file_put_contents ($file, $sTR);    }} return TRUE;        }/********************** initialization Property **********************/Private Function SetAttribute ($tableName, $moduleName) {        /**************** Initialization Property $modulename ****************/$this->modulename = $moduleName;        /**************** initialization attribute $truetabelname ****************///judgment if no table prefix is added $prefix = C (' Db_prefix ');        if (Strpos ($tableName, $prefix)!== 0) $this->truetabelname = $prefix. $tableName;        else $this->truetabelname = $tableName;            /**************** Initialization Property $tpname ****************///Remove Table prefix if (Strpos ($tableName, $prefix) = = = 0) {            $len = strlen ($prefix);        Start by intercepting the table name from the prefix to the last $tableName = substr ($this->truetabelname, $len);        }//To drop the dash $tableName = Explode (' _ ', $tableName);        The first element in $tablename is treated with ucfirst $tableName = Array_map (' Ucfirst ', $tableName); $tableName = Implode ("', $taBlename);        $this->tpname = $tableName; /**************** Initialization Properties $fields ****************///Remove all field information $this->fields = $this->query (' SHOW full        Fields from '. $this->truetabelname); /**************** Initialization Property $tablecomment ****************/$result = $this->query ("Select Table_comment from Informa Tion_schema. TABLES WHERE table_schema= ' ".        C (' db_name '). "' and Table_name= '". $this->truetabelname. "'");        $this->tablecomment = $result [0][' table_comment '];    return TRUE;        }/********************** Create module directory **********************/public Function BuildDir ($module) {//created automatically if not created if (is_writeable (App_path)) {$dirs = Array (app_path. $module. ' /', App_path. $module. ' /common/', App_path. $module. ' /controller/', App_path. $module. ' /model/', App_path. $module. ' /conf/', App_path. $module. '            /view/',); FOreach ($dirs as $dir) {if (!is_dir ($dir)) mkdir ($dir, 0755,true); }        }    }}

CqhController.class.php

<?phpnamespace cqh\controller;use think\controller;class Cqhcontroller extends controller{public Function index ()   {if (is_post) {$tableName = I (' Post.tablename ');    The table name in the receiving form is $moduleName = Ucfirst (I (' post.modulename ')); Receive module name $generaType = I (' Post.generatype ');//receive the type you want to generate $this->validate ($tableName, $moduleName, $ge            Neratype);//Verify form $gModel = D (' Cqh '); $gModel->iniset ($tableName, $moduleName); Initialize, incoming table name and module name if ($generaType [' Controller ']) $gModel->gcontroller ();  Build controller if ($generaType [' model ']) $gModel->gmodel ();   Generate model if ($generaType [' View ']) $gModel->gview ();            Generate View $this->success (' Build succeeded ');        Exit    } $this->display (); The public function validate ($tableName, $moduleName, $generaType) {if (!) (            (Preg_match ('/\s+/', $tableName) = = = 1))) $this->error (' Please enter table name '); if (! (            (Preg_match ('/\s+/', $moduleName) = = = 1)))        $this->error (' Please enter the module name ');    if (! $generaType) $this->error (' Please select the type to generate '); }}

view/cqh/index.html

<!doctype html>

Self-written thinkphp automatically generated classes

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.