php 讀取表結構自動產生php類

來源:互聯網
上載者:User
平時寫項目的時候經常會碰到操作資料庫,每次操作資料庫的時候最麻煩的就是要寫一個表映射的類,根據規律總結寫了一個自動產生的小demo:

// 資料庫配置參數$db_config = array(    'host' => '127.0.0.1',    'port' => 3306,    'username' => 'root',    'password' => '',    'dbname' => 'joe_db',    'charset' => 'utf8');$tablePre = 'joe_'; // 表首碼$tableName = 'users'; // 表名$res = linkdb($db_config, $tablePre . $tableName);$className = $tableName . 'Dmodel';$fileName = $tableName . '.dm.class.php';$annStr = addAnnotation($fileName, $tableName, '使用者session表');$classStr = readTableStr($res, $className,$annStr);// 寫入檔案$file = fopen($fileName, "w+");fwrite($file, $classStr);fclose($file);/** * 根據表結構初始化表對象 * * @param unknown $res             * @param unknown $className             * @return string */functionreadTableStr($res, $className, $annStr=''){$result = ' . "\n";    $result .= $annStr;    $result .= "class $className \n{";    // 變數foreach ($resas$v) {        $result .= "\n" . 'private $_' . strtolower($v['Field']) . ";//" . $v['Comment'] . $v['Type'];    }    $result .= "\n";    // get setforeach ($resas$v) {        $result .= "\n public function set" . $v['Field'] . '($_' . strtolower($v['Field']) . "){";        $result .= "\n" . ' $this->_' . strtolower($v['Field']) . '=$_' . strtolower($v['Field']) . ";";        $result .= "\n }";        $result .= "\n public function get" . $v['Field'] . '(){';        $result .= "\n" . ' return $this->_' . strtolower($v['Field']) . ";";        $result .= "\n }";        $result .= "\n";    }    $result .= " \n }";    $result .= " \n ?>";    return$result;}/** * 添加類注釋 *  * @param unknown $fileName             * @param unknown $fun             * @param unknown $des             * @param unknown $date             * @param unknown $author             */functionaddAnnotation($fileName, $fun, $des){$annStr =  "\n/**";    $annStr .= "\n* 檔案名稱:" . $fileName;    $annStr .= "\n* 功能:    模型層-表-" . $fun;    $annStr .= "\n* 描述:    " . $des;    $annStr .= "\n* 日期:    " . date('y-m-d', time());    $annStr .= "\n* 著作權:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved";    $annStr .= "\n* @author JoeXiong";    $annStr .= "\n*/";    return$annStr;}/** * * 串連資料庫,查詢表結構 * * @param unknown $array *            資料庫連接參數 * @param unknown $tableName *            表名 */functionlinkdb($array, $tableName){$mysql_server_name = $array['host']; // 改成自己的mysql資料庫伺服器$mysql_username = $array['username']; // 改成自己的mysql資料庫使用者名稱$mysql_password = $array['password']; // 改成自己的mysql資料庫密碼$mysql_database = $array['dbname']; // 改成自己的mysql資料庫名$conn = mysql_connect($mysql_server_name, $mysql_username, $mysql_password) ordie("error connecting"); // 串連資料庫    mysql_query("set names " . $array['charset']); // 資料庫輸出編碼    mysql_select_db($mysql_database); // 開啟資料庫$sql = "SHOW FULL COLUMNS FROM $tableName";    $res = mysql_query($sql);    $res = dataTable($res);    return$res;}/** * 返回到表集合 * * @param query $query             * @return array */functiondataTable($query){if ($query) {        $ListTable = array();        while ($rows = mysql_fetch_array($query, MYSQL_ASSOC)) {            array_push($ListTable, $rows);        }        return$ListTable;    } else {        return0;    }}/** * 調試函數 * * @param unknown $msg             */functionshow_bug($msg){echo'
';    var_dump($msg);    echo'
';}

最終實現效果:

/*** 檔案名稱:users.dm.class.php* 功能:    模型層-表-users* 描述:    使用者session表* 日期:    16-06-27* 著作權:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved* @author JoeXiong*/classusersDmodel{private$_userid; // int(11) unsignedprivate$_username; // 使用者名稱varchar(50)private$_password; // 密碼varchar(32)private$_email; // 郵箱varchar(80)private$_mobile; // 手機號碼varchar(20)private$_ip; // 使用者註冊IDvarchar(30)private$_status; // 帳號狀態tinyint(1) unsignedprivate$_areaid; // 地區Id,10001為本區;10002為qq使用者;10003為新浪微博使用者smallint(5) unsignedprivate$_addtime; // 建立時間int(10) unsignedpublicfunctionsetUserId($_userid)    {$this->_userid = $_userid;    }    publicfunctiongetUserId()    {return$this->_userid;    }    publicfunctionsetUserName($_username)    {$this->_username = $_username;    }    publicfunctiongetUserName()    {return$this->_username;    }    publicfunctionsetPassWord($_password)    {$this->_password = $_password;    }    publicfunctiongetPassWord()    {return$this->_password;    }    publicfunctionsetEmail($_email)    {$this->_email = $_email;    }    publicfunctiongetEmail()    {return$this->_email;    }    publicfunctionsetMobile($_mobile)    {$this->_mobile = $_mobile;    }    publicfunctiongetMobile()    {return$this->_mobile;    }    publicfunctionsetIP($_ip)    {$this->_ip = $_ip;    }    publicfunctiongetIP()    {return$this->_ip;    }    publicfunctionsetStatus($_status)    {$this->_status = $_status;    }    publicfunctiongetStatus()    {return$this->_status;    }    publicfunctionsetAreaId($_areaid)    {$this->_areaid = $_areaid;    }    publicfunctiongetAreaId()    {return$this->_areaid;    }    publicfunctionsetAddTime($_addtime)    {$this->_addtime = $_addtime;    }    publicfunctiongetAddTime()    {return$this->_addtime;    }}?>

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介紹了 php 讀取表結構自動產生php類,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.