Intelligent Wizard to Create Database Models

Last modified by dsoprea on Tue, September 6, 2011 18:13
Source|Old Revisions  

o Will allow you to simply add models for new tables in the main Magento database, or add models for tables on a different database defined in addition to the main Magento one in local.xml . o Will completely manage/fix casing issues in the given module names, class names, model names, etc... The modules that are given for the new models can be in any code-pool (community, local, etc..). o If any of the files or XML that this routine tries to create is already there, it will be checked for correctness. If the process fails, this will allow you to fix whatever problem you had and rerun to pick-up where you left off.

The code is here:

You may call it like the following (this uses the default database for Magento):

  1.     $modulePath = '/var/vhosts/magento/app/code/local/Module/Project';
  2.     $moduleName = 'Module_Project';
  3.     $modelName  = 'newmodel';
  5.     // $entityname is the name of the node that is added in the
  6.     // "<entities />" section in the main config file where the
  7.     // model is configured.
  8.     $entityName = 'swatches';
  9.     $tableName  = 'ColorSwatches';
  10.     $pkColumn   = 'ColorSwatchId';
  12.     $errorMessage = null;
  13.     if(($obj = MagentoAddModule::Factory($modulePath, $moduleName, $modelName, $errorMessage)) === false)
  14.         die("Could not manufacture add-module: $errorMessage");
  16.     if($obj->installTableModel($entityName, $tableName, $pkColumn) == false)
  17.         die("Could not create model.nn");

You can use a different database defined in local.xml by implementing MagentoDbConnection and passing it into installTableModel():

  1.     $dbInfo = new MagentoDbConnection();
  3.     if($dbInfo->setConnectionNameOverride('readconnection_xmlgroupname', 'writeconnection_xmlgroupname') == false)
  4.         die("Could not set connection NAMES.nn");
  6.     if($obj->installTableModel($entityName, $tableName, $pkColumn, $dbInfo) == false)
  7.         die("Could not create model.nn");

You find code at the bottom of the file that will allow you to use it as a script. You may pass parameters to it or it will prompt you if nothing is passed at all.

Dustin Oprea