モデルのフィールド情報ファイルを作成

投稿者 aoki 2009 年 12 月 12 日 – 9:50 PM -

モデルのフィールド情報ファイルを作成する。(PostgreSQLのみ対応。)

コマンド ( 使う前に cake に path を通しておく)
cake -app /var/www/html/cake_1.2.5/app/ make_table_info

出力先 table_infos ディレクトリを作成 書き込み権限を付与
/var/www/html/cake_1.2.5/app/tmp/table_infos/

モデル名.txt で出力される。

PHP:
  1. class MakeTableInfoShell extends Shell
  2. {
  3.     var $uses = array("Tbl0300Request","Tbl0100Corporation");
  4.     var $controller = true;
  5.  
  6.     function main()
  7.     {
  8.         foreach ($this->uses as $model) {
  9.  
  10.             //$model = $this->modelClass;
  11.             $sql = sprintf("select
  12.                            attr.attname     as column_name
  13.                            ,attr.attnotnull  as not_null
  14.                            ,des2.description as column_comment
  15.                         from
  16.                           pg_catalog.pg_class a inner join pg_catalog.pg_namespace   ns   on a.relnamespace = ns.oid
  17.                                                 left  join pg_catalog.pg_description des  on a.oid          = des.objoid
  18.                                                                                          and des.objsubid   = 0
  19.                                                 inner join pg_catalog.pg_attribute   attr on a.oid          = attr.attrelid
  20.                                                                                          and attr.attnum> 0
  21.                                                 left  join pg_catalog.pg_description des2 on a.oid          = des2.objoid
  22.                                                                                          and attr.attnum    = des2.objsubid
  23.                         where
  24.                             a.relkind IN ('r', 'v')
  25.                         and ns.nspname IN ('public')
  26.                         and a.relname='%s'
  27.                         order by
  28.                             ns.oid
  29.                            ,a.oid
  30.                            ,attr.attnum"
  31.             ,$this->$model->useTable
  32.             );
  33.  
  34.             $result = $this->$model->query( $sql );
  35.             array_walk($result,array($this,'convert_encoding'));
  36.  
  37.             $fp = fopen("tmp/table_infos/$model.txt", 'w');
  38.  
  39.             fwrite($fp, 'function _set_table_info_idxs(){
  40.             ');
  41.             fwrite($fp, '$i=0;
  42.             ');
  43.             $i=0;
  44.             foreach ($result as $value) {
  45.  
  46.                 if ($i) {
  47.                     fwrite($fp, '$i++;
  48.                 ');
  49.                 }
  50.                 fwrite($fp, '$model[ $i ] = "' . $model .'";
  51.                 ');
  52.                 fwrite($fp, '$filed[ $i ] = $s_filed[ $i ] = "' . $value[0]["column_name"] .'";
  53.                 ');
  54.                 if (trim($value[0]["column_comment"])=="") {
  55.                     $value[0]["column_comment"]= strtoupper($value[0]["column_name"]);
  56.                 }
  57.                 fwrite($fp, '$label[ $i ] = "' . $value[0]["column_comment"]  .'";
  58.                 ');
  59.                 fwrite($fp, '$show_index[ $i ] = false;
  60.                 ');
  61.                 fwrite($fp, '$width[ $i ] = 55;
  62.                 ');
  63.                 if ($value[0]["not_null"]) {
  64.                     fwrite($fp, '$hissu[ $i ] = true;
  65.                 ');
  66.                 } else {
  67.                     fwrite($fp, '$hissu[ $i ] = false;
  68.                 ');
  69.                 }
  70.                 fwrite($fp, "
  71.                 ");
  72.  
  73.                 $i++;
  74.             }
  75.  
  76.             fwrite($fp, '$this->table_info_idxs  = $this->_set_help_table_info_idxs( $model, $filed , $s_filed , $label, @$link , @$show_index ,@$width);
  77.             ');
  78.             fwrite($fp, '}
  79.        
  80.             ');
  81.            
  82.             fclose($fp);
  83.         }
  84.     }
  85.  
  86.  
  87.     function convert_encoding(&$item, $key){
  88.  
  89.         $encoding=array("to"=>"UTF-8","from"=>"EUC-JP");
  90.  
  91.         if (is_array($item)){
  92.             array_walk($item, array($this,'convert_encoding'), $encoding);
  93.         } else {
  94.             $item = mb_convert_encoding($item, $encoding['to'], $encoding['from']);
  95.         }
  96.     }
  97.  
  98. }


Tags:
カテゴリー: PHP | トラックバックURL

コメントをどうぞ