Source for file generateentity.php

Documentation is available at generateentity.php

  1. <?php
  2.  
  3. require_once 'creole/Creole.php';
  4.  
  5. /* fix $dsn, $datasource, $outputdir to suit your environment */
  6. $dsn array('phptype' => 'mysql',
  7.              'hostspec' => 'localhost',
  8.              'username' => 'default',
  9.              'password' => 'default',
  10.              'database' => 'default');
  11. $datasource "default";
  12. $outputdir dirname(__FILE__)."/../../../../components/entity";
  13.  
  14. /*
  15.  * start. 
  16.  */
  17. $conn Creole::getConnection($dsn);
  18. $dbinfo $conn->getDatabaseInfo();
  19.  
  20. $base_dir $outputdir .'/''base';
  21. if (file_exists($base_dir&& mkdir($base_dir)) {
  22.     print "could not make directory for base: {$outputdir}";
  23.     exit;
  24. }
  25.  
  26. foreach($dbinfo->getTables(as $tbl{
  27.     $classname "";
  28.     $tablename "";
  29.     $capName "";
  30.     $pkdef "";
  31.     $coldef "";
  32.     $joindef "";
  33.     $aliasesdef "";
  34.     
  35.     $tablename strtolower($tbl->getName());
  36.     $ar split('_'$tablename);
  37.     foreach($ar as $key => $val{
  38.         $ar[$keyucfirst($val);
  39.     }
  40.     $capName join(''$ar);
  41.     $classname "Entity_{$capName}";
  42.     $baseclassname "Entity_Base_{$capName}";
  43.  
  44.     // columns
  45.     $cols array();
  46.     foreach($tbl->getColumns(as $col{
  47.         $cols[strtolower($col->getName());
  48.     }
  49.     
  50.     // primary key
  51.     $pks array();
  52.     $pk $tbl->getPrimaryKey();
  53.     if (is_object($pk)) {
  54.         foreach($pk->getColumns(as $pkcol{
  55.             $pks[strtolower($pkcol->getName());
  56.         }
  57.     }
  58.     
  59.     // join
  60.     $aliases array();
  61.     $joindef_buf array();
  62.     $fks $tbl->getForeignKeys();
  63.     if (is_array($fks)) {
  64.         foreach($fks as $fk{
  65.             $alias "";
  66.             $entity "";
  67.             $refdef array();
  68.             
  69.             $refs $fk->getReferences();
  70.             if (is_array($refs)) {
  71.                 //$alias = strtolower($fk->getName());
  72.                 $alias strtolower($refs[0][1]->getTable()->getName());
  73.                 $entity 'Entity_'ucfirst(strtolower($refs[0][1]->getTable()->getName()));
  74.                 $aliases[array(
  75.                     'name' => $alias,
  76.                     'entity' => $entity
  77.                 );
  78.                 
  79.                 foreach($refs as $ref{
  80.                     $p strtolower($ref[0]->getName());
  81.                     $c strtolower($ref[1]->getName());
  82.                     $refdef["                '{$p}' => '{$c}'";
  83.                 }
  84.                 
  85.                 $buf "";
  86.                 $buf .= "        '{$alias}' => array(\n";
  87.                 $buf .= "            'entity' => '{$entity}',\n";
  88.                 $buf .= "            'type' => 'INNER JOIN',\n";
  89.                 $buf .= "            'relation' => array(\n";
  90.                 $buf .= implode(",\n"$refdef);
  91.                 $buf .= "\n";
  92.                 $buf .= "            )\n";
  93.                 $buf .= "        )";
  94.                 
  95.                 $joindef_buf[$buf;
  96.             }
  97.         }
  98.     }
  99.     if (count($joindef_buf)) {
  100.         $joindef implode(",\n"$joindef_buf);
  101.     }
  102.     
  103.     if (count($aliases)) {
  104.         foreach ($aliases as $alias{
  105.             $aliasesdef .= <<<EOT
  106.     /**
  107.      * @var {$alias['entity']
  108.      */
  109.     public \${$alias['name']};
  110.  
  111.  
  112. EOT;
  113.         }
  114.     }
  115.     
  116.     //$cols = array_diff($cols, $pks);
  117.     foreach ($cols as $col{
  118.         $coldef .= "    public \$".$col.";\n";
  119.     }
  120.     
  121.     //$coldef = "'". implode("',\n        '", $cols) ."'";
  122.     $pkdef "'"implode("',\n'"$pks."'";
  123.  
  124.     // ベースクラスの作成
  125.     $filepath $outputdir ."/base/{$capName}.php";
  126.     if (file_exists($filepath)) {
  127.         print "{$tablename}: base class already exists. \n";
  128.         continue;
  129.     }
  130.     if (!$handle fopen($filepath'wb')) {
  131.         print "{$filepath}: could not open file. \n";
  132.         continue;
  133.     }
  134.         
  135.     $contents = <<<EOT
  136. class {$baseclassname} extends Teeple_ActiveRecord
  137. {
  138.  
  139.     /**
  140.      * 使用するデータソース名を指定します。
  141.      * 指定が無い場合は、DEFAULT_DATASOURCE で設定されているDataSource名が使用されます。
  142.      *
  143.      * @var string
  144.      */
  145.     public static \$_DATASOURCE = '{$datasource}';
  146.     
  147.     /**
  148.      * このエンティティのテーブル名を設定します。
  149.      * 
  150.      * <pre>
  151.      * スキーマを設定する場合は、"スキーマ.テーブル名"とします。
  152.      * 子クラスにて必ずセットする必要があります。
  153.      * </pre>
  154.      *
  155.      * @var string
  156.      */
  157.     public static \$_TABLENAME = '{$tablename}';
  158.     
  159.     /**
  160.      * プライマリキー列を設定します。
  161.      * 
  162.      * <pre>
  163.      * プライマリキーとなるカラム名を配列で指定します。
  164.      * 子クラスにて必ずセットする必要があります。
  165.      * </pre>
  166.      * 
  167.      * @var array 
  168.      */
  169.     public static \$_PK = array(
  170.         {$pkdef}
  171.     );
  172.     
  173.     /**
  174.      * このテーブルのカラム名をpublicプロパティとして設定します。(プライマリキーを除く)
  175.      * <pre>
  176.      * 子クラスにて必ずセットする必要があります。
  177.      * </pre>
  178.      */
  179. {$coldef}
  180.     
  181.     /**
  182.      * プライマリキーが自動セット(auto increment)かどうかを設定します。
  183.      * 
  184.      * <pre>
  185.      * 子クラスにて必ずセットする必要があります。
  186.      * </pre>
  187.      * 
  188.      * @var bool 
  189.      */
  190.     public static \$_AUTO = TRUE;
  191.  
  192. }
  193. EOT;
  194.  
  195.     $contents "<?php \n\n"$contents ."\n\n?>";
  196.     if (fwrite($handle$contents=== FALSE{
  197.         print "{$filepath}: failed to write to the file. \n";
  198.         continue;
  199.     }
  200.  
  201.     print "{$tablename}: entity base class created . \n";
  202.     fclose($handle);
  203.  
  204.     // エンティティクラスの作成
  205.     $filepath $outputdir ."/{$capName}.php";
  206.     if (file_exists($filepath)) {
  207.         print "{$tablename}: entity class already exists. \n";
  208.         continue;
  209.     }
  210.     if (!$handle fopen($filepath'wb')) {
  211.         print "{$filepath}: could not open file. \n";
  212.         continue;
  213.     }
  214.         
  215.     $contents = <<<EOT
  216. /**
  217.  * Entity Class for {$tablename}
  218.  *
  219.  * エンティティに関するロジック等はここに実装します。
  220.  * @package entity
  221.  */
  222. class {$classname} extends {$baseclassname}
  223. {
  224.     /**
  225.      * インスタンスを取得します。
  226.      * @return {$classname}
  227.      */
  228.     public static function get() {
  229.         return Teeple_Container::getInstance()->getEntity('{$classname}');
  230.     }
  231.     
  232.     /**
  233.      * 単一行の検索を実行します。
  234.      * @param \$id
  235.      * @return {$classname}
  236.      */
  237.     public function find(\$id=null) {
  238.         return parent::find(\$id);
  239.     }
  240.     
  241.     /**
  242.      * JOINするテーブルを設定します。
  243.      * ※generatorが吐き出した雛形を修正してください。
  244.      * 
  245.      * ここに設定してある定義は、\$this->join('aliasname') で利用できる。<br/>
  246.      * ※ここに設定しただけではJOINされない。
  247.      * 
  248.      * <pre>
  249.      * 指定方法: 'アクセスするための別名' => 設定値の配列
  250.      * 設定値の配列:
  251.      *   'entity' => エンティティのクラス名
  252.      *  'columns' => 取得するカラム文字列(SQLにセットするのと同じ形式)
  253.      *   'type' => JOINのタイプ(SQLに書く形式と同じ)(省略した場合はINNER JOIN)
  254.      *   'relation' => JOINするためのリレーション設定
  255.      *      「本クラスのキー名 => 対象クラスのキー名」となります。
  256.      *   'condition' => JOINするための設定だがリテラルで指定するもの
  257.      * 
  258.      * 値の例:
  259.      * 
  260.      * \$join_config = array(
  261.      *     'aliasname' => array(
  262.      *         'entity' => 'Entity_Fuga',
  263.      *         'columns' => 'foo, bar, hoge',
  264.      *         'type' => 'LEFT JOIN',
  265.      *         'relation' => array(
  266.      *             'foo_id' => 'bar_id'
  267.      *         ),
  268.      *         'condition' => 'aliasname.status = 1 AND parent.status = 1'
  269.      *     )
  270.      * );
  271.      * </pre>
  272.      * 
  273.      * @var array
  274.      */
  275.     public static \$_JOINCONFIG = array(
  276. {$joindef}
  277.     );
  278.     
  279. {$aliasesdef}    
  280.  
  281. }
  282. EOT;
  283.  
  284.     $contents "<?php \n\n"$contents ."\n\n?>";
  285.     if (fwrite($handle$contents=== FALSE{
  286.         print "{$filepath}: failed to write to the file. \n";
  287.         continue;
  288.     }
  289.  
  290.     print "{$tablename}: entity class created . \n";
  291.     fclose($handle);
  292.     
  293. }
  294.  
  295. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:50 +0900 by phpDocumentor 1.4.3