SchemaBuilder.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: SchemaBuilder.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class SchemaBuilder extends BaseBuilder
00017     {
00018         public static function buildTable($tableName, /* array */ $propertyList)
00019         {
00020             $out = <<<EOT
00021 \$schema->
00022     addTable(
00023         DBTable::create('{$tableName}')->
00024 
00025 EOT;
00026 
00027             $columns = array();
00028             
00029             foreach ($propertyList as $property) {
00030                 if (
00031                     $property->getRelation()
00032                     && ($property->getRelationId() != MetaRelation::ONE_TO_ONE)
00033                 ) {
00034                     continue;
00035                 }
00036                 
00037                 $column = $property->toColumn();
00038                 
00039                 if (is_array($column))
00040                     $columns = array_merge($columns, $column);
00041                 else
00042                     $columns[] = $property->toColumn();
00043             }
00044             
00045             $out .= implode("->\n", $columns);
00046             
00047             return $out."\n);\n\n";
00048         }
00049         
00050         public static function buildRelations(MetaClass $class)
00051         {
00052             $out = null;
00053             
00054             foreach ($class->getAllProperties() as $property) {
00055                 if ($relation = $property->getRelation()) {
00056                     
00057                     $foreignClass = $property->getType()->getClass();
00058                     
00059                     if (
00060                         $relation->getId() == MetaRelation::ONE_TO_MANY
00061                         // nothing to build, it's in the same table
00062                         // or table does not exist at all
00063                         || !$foreignClass->getPattern()->tableExists()
00064                         // no need to process them
00065                         || $class->getParent()
00066                     ) {
00067                         continue;
00068                     } elseif (
00069                         $relation->getId() == MetaRelation::MANY_TO_MANY
00070                     ) {
00071                         $tableName =
00072                             $class->getTableName()
00073                             .'_'
00074                             .$foreignClass->getTableName();
00075                         
00076                         $foreignPropery = clone $foreignClass->getIdentifier();
00077                         
00078                         $name = $class->getName();
00079                         $name = strtolower($name[0]).substr($name, 1);
00080                         $name .= 'Id';
00081                         
00082                         $foreignPropery->
00083                             setName($name)->
00084                             setColumnName($foreignPropery->getConvertedName())->
00085                             // we don't need primary key here
00086                             setIdentifier(false);
00087                         
00088                         // we don't want any garbage in such tables
00089                         $property = clone $property;
00090                         $property->required();
00091                         
00092                         // prevent name collisions
00093                         if (
00094                             $property->getRelationColumnName()
00095                             == $foreignPropery->getColumnName()
00096                         ) {
00097                             $foreignPropery->setColumnName(
00098                                 $class->getTableName().'_'
00099                                 .$property->getConvertedName().'_id'
00100                             );
00101                         }
00102                         
00103                         $out .= <<<EOT
00104 \$schema->
00105     addTable(
00106         DBTable::create('{$tableName}')->
00107         {$property->toColumn()}->
00108         {$foreignPropery->toColumn()}->
00109         addUniques('{$property->getRelationColumnName()}', '{$foreignPropery->getColumnName()}')
00110     );
00111 
00112 
00113 EOT;
00114                     } else {
00115                         $sourceTable = $class->getTableName();
00116                         $sourceColumn = $property->getRelationColumnName();
00117                         
00118                         $targetTable = $foreignClass->getTableName();
00119                         $targetColumn = $foreignClass->getIdentifier()->getColumnName();
00120                         
00121                         $out .= <<<EOT
00122 // {$sourceTable}.{$sourceColumn} -> {$targetTable}.{$targetColumn}
00123 \$schema->
00124     getTableByName('{$sourceTable}')->
00125         getColumnByName('{$sourceColumn}')->
00126             setReference(
00127                 \$schema->
00128                     getTableByName('{$targetTable}')->
00129                     getColumnByName('{$targetColumn}'),
00130                 ForeignChangeAction::restrict(),
00131                 ForeignChangeAction::cascade()
00132             );
00133 
00134 
00135 EOT;
00136                     
00137                     }
00138                 }
00139             }
00140             
00141             return $out;
00142         }
00143         
00144         public static function getHead()
00145         {
00146             $out = parent::getHead();
00147             
00148             $out .= "\$schema = new DBSchema();\n\n";
00149             
00150             return $out;
00151         }
00152     }
00153 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4