Ignore:
Timestamp:
Mar 27, 2012, 4:11:51 PM (13 years ago)
Author:
chronos
Message:
  • Upraveno: Vylepšeno generování modulu pro OpenERP.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/temp/MakeOpenERP.php

    r399 r400  
    3737
    3838$Database->select_db('information_schema');
     39
     40// Load all constraints
     41$Constraints = array();
     42$DbResult = $Database->query('SELECT * FROM `KEY_COLUMN_USAGE` WHERE `TABLE_SCHEMA` = "centrala" AND
     43    `REFERENCED_TABLE_NAME` IS NOT NULL');
     44while($DbRow = $DbResult->fetch_assoc())
     45{   
     46  $Constraints[strtolower($DbRow['TABLE_NAME'].'.'.$DbRow['COLUMN_NAME'])] =
     47    strtolower($DbRow['REFERENCED_TABLE_NAME'].'.'.$DbRow['REFERENCED_COLUMN_NAME']);
     48}
     49
    3950$DbResult = $Database->query('SELECT * FROM `TABLES` WHERE `TABLE_SCHEMA` = "centrala"');
    4051while($DbRow = $DbResult->fetch_assoc())
     
    5364    $Type = $DbRow2['DATA_TYPE'];
    5465    $Name = $DbRow2['COLUMN_NAME'];
    55  
    56     $ColDef = "    '".$Name."': fields.";
     66    if($Name != 'Id')
     67    {
     68    $ColDef = "    '".strtolower($Name)."': fields.";
    5769    if($Type == 'varchar')
    5870     $ColDef .= "char('".$Name."', size=".$DbRow2['CHARACTER_MAXIMUM_LENGTH'].", translate=True";
     
    6173    else if($Type == 'date')
    6274     $ColDef .= "date('".$DbRow2['COLUMN_NAME']."'";
    63     else if($Type == 'int')
    64      $ColDef .= "integer('".$DbRow2['COLUMN_NAME']."'";
    65     else if($Type == 'tinyint')
    66      $ColDef .= "integer('".$DbRow2['COLUMN_NAME']."'";
    67     else if($Type == 'smallint')
    68      $ColDef .= "integer('".$DbRow2['COLUMN_NAME']."'";
     75    else if(($Type == 'int') or ($Type == 'tinyint') or ($Type == 'smallint'))
     76    {
     77      if(array_key_exists(strtolower($DbRow2['TABLE_NAME'].'.'.$DbRow2['COLUMN_NAME']), $Constraints))
     78      {
     79        $Constraint = $Constraints[strtolower($DbRow2['TABLE_NAME'].'.'.$DbRow2['COLUMN_NAME'])];
     80        $SourceTable = substr($Constraint, 0, strpos($Constraint, '.'));
     81        $SourceColumn = substr($Constraint, strpos($Constraint, '.') + 1);
     82        $ColDef .= "many2one('centrala.".$SourceTable."', '".$DbRow2['COLUMN_NAME']."'";
     83      } else
     84       $ColDef .= "integer('".$DbRow2['COLUMN_NAME']."'";
     85    }
    6986    else if($Type == 'enum')
    7087     $ColDef .= "integer('".$DbRow2['COLUMN_NAME']."'";
     
    7895     $ColDef .= "float('".$DbRow2['COLUMN_NAME']."', digits=(12,6)";
    7996    else echo('Neznamy typ '.$Type);
     97    $ColDef .= "),\n";
     98    $ModelColumns .= $ColDef;
     99      $ViewTreeItems .= "\n".'          <field name="'.strtolower($Name).'" select="1"/>';
     100      $ViewFormItems .= "\n".'          <field name="'.strtolower($Name).'" select="1"/>';
     101    }
     102  }
     103  // Add one2many relations
     104  foreach($Constraints as $Index => $Item)
     105  {
     106    //echo($Item.'='.strtolower($DbRow['TABLE_NAME']).'.Id ');
     107    if($Item == strtolower($DbRow['TABLE_NAME']).'.id')     
     108    {
     109      $SourceTable = substr($Index, 0, strpos($Index, '.'));
     110      $SourceColumn = substr($Index, strpos($Index, '.') + 1);
     111      $ColDef = "    '".strtolower($SourceTable.'_ids')."': fields.";
     112        $ColDef .= "one2many('centrala.".$SourceTable."', '".$SourceColumn."', '".$SourceTable."'";         
     113      $ColDef .= "),\n";
     114      $ModelColumns .= $ColDef;
     115      $ViewFormItems .= "\n".'          <field name="'.strtolower($SourceTable.'_ids').'" select="1"/>';
     116    }
     117  }
    80118   
    81     $ColDef .= "),\n";
    82     $ViewTreeItems .= "\n".'          <field name="'.$Name.'" select="1"/>';
    83     $ViewFormItems .= "\n".'          <field name="'.$Name.'" select="1"/>';
    84     $ModelColumns .= $ColDef;
    85   }
    86119  $ModuleView .= '
    87120    <record model="ir.actions.act_window" id="action_'.$TableName.'_form">
Note: See TracChangeset for help on using the changeset viewer.