Пример #1
0
 public function create_model_code($class)
 {
     $table = $this->table_name;
     $package = $this->module_name;
     $app_module = new \app\App_Module();
     $db = mysqli_connect($app_module->get_module_config("db_host"), $app_module->get_module_config("db_user"), $app_module->get_module_config("db_psw"));
     $importClass = "";
     $assocFields = "";
     $assocFieldFuncs = "";
     mysqli_select_db($db, "INFORMATION_SCHEMA");
     $result = mysqli_query($db, "select COLUMN_NAME,CONSTRAINT_NAME,\r\n\t\tREFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE\r\n\t\twhere TABLE_SCHEMA = '" . $app_module->get_module_config("db_name") . "' and TABLE_NAME = '{$table}'\r\n\t\tand referenced_column_name is not NULL");
     while ($row = mysqli_fetch_assoc($result)) {
         $col = rtrim($row['REFERENCED_TABLE_NAME'], "s");
         //先假定复数形式
         $col_class = $this->get_class_of_table($row['REFERENCED_TABLE_NAME']);
         $importClass .= "use {$col_class};\r\n";
         $sortClass = substr($col_class, strripos($col_class, "\\") + 1);
         $assocFields .= "\r\n\tprivate \${$col};\r\n";
         $assocFieldFuncs .= "\r\n\tpublic function get_{$col}(){\r\n\t\tif( ! \$this->{$col}){\r\n\t\t\t\$this->{$col} = {$sortClass}::find_by_id(\$this->get(self::F_" . strtoupper($row['COLUMN_NAME']) . "));\r\n\t\t}\r\n\t\treturn \$this->{$col};\r\n\t}\r\n\t\r\n\t/**\r\n\t * @return {$class}\r\n\t */\r\n\tpublic function set_{$col}({$sortClass} \$new){\r\n\t\t\$this->{$col} = \$new;\r\n\t\treturn \$this;\r\n\t}\r\n";
     }
     mysqli_select_db($db, $app_module->get_module_config("db_name"));
     mysqli_query($db, "set names utf8");
     $unique_key = array();
     $result = mysqli_query($db, "SHOW INDEX FROM  {$table}");
     while ($row = mysqli_fetch_assoc($result)) {
         $unique_key[$row['Column_name']] = $row['Key_name'];
     }
     $constant = array();
     $result = mysqli_query($db, "show full columns from {$table}");
     while ($row = mysqli_fetch_assoc($result)) {
         $row['Key'] == "PRI" ? $key = $row['Field'] : null;
         $type_info = $this->get_type_info($row['Type']);
         $constant = array_merge((array) $constant, (array) $this->getEnumConstant($row['Field'], $row['Type']));
         @($fielddefine .= "       " . str_pad("'" . $row['Field'] . "'", 12, " ") . " => array('type' => '" . $type_info['type'] . "', 'null' => " . (strcasecmp($row['Null'], "YES") ? "false" : "true") . ",'length' => '" . $type_info['length'] . "','default'\t=> '" . $row['Default'] . "',),\r\n");
         @($properConst .= "\r\n    /**\r\n     * {$row['Comment']}\r\n     * @var {$type_info['type']}\r\n     */\r\n    const F_" . strtoupper($row['Field']) . " = \"{$row['Field']}\";");
     }
     $constantdefine = '';
     foreach ($constant as $c => $v) {
         $constantdefine .= "\r\n    const {$v} = '{$c}';";
     }
     return "<?php\r\nnamespace app\\{$package};\r\nuse \\yangzie\\YZE_Model;\r\nuse \\yangzie\\YZE_SQL;\r\nuse \\yangzie\\YZE_DBAException;\r\nuse \\yangzie\\YZE_DBAImpl;\r\n{$importClass}\r\n/**\r\n*\r\n*\r\n* @version \$Id\$\r\n* @package {$package}\r\n*/\r\nclass {$class} extends YZE_Model{\r\n    {$constantdefine}\r\n    const TABLE= \"{$table}\";\r\n    const VERSION = 'modified_on';\r\n    const MODULE_NAME = \"{$package}\";\r\n    const KEY_NAME = \"{$key}\";\r\n    const CLASS_NAME = 'app\\{$package}\\{$class}';\r\n    {$properConst}\r\n    public static \$columns = array(\r\n        {$fielddefine}\r\n    );\r\n    //array('attr'=>array('from'=>'id','to'=>'id','class'=>'','type'=>'one-one||one-many') )\r\n    //\$this->attr\r\n    protected \$objects = array();\r\n    /**\r\n     * @see YZE_Model::\$unique_key\r\n     */\r\n    protected \$unique_key = " . var_export($unique_key, true) . ";\r\n    \t\t\r\n    {$assocFields}\r\n\t{$assocFieldFuncs}\r\n}?>";
 }
Пример #2
0
function is_validate_table($table)
{
    global $db;
    $app_module = new \app\App_Module();
    $db = mysqli_connect($app_module->get_module_config("db_host"), $app_module->get_module_config("db_user"), $app_module->get_module_config("db_psw"));
    mysqli_select_db($db, $app_module->get_module_config("db_name"));
    return mysqli_query($db, "show full columns from {$table}");
}