ModelDescriptor::setTable PHP Method

setTable() public method

public setTable ( $table, $loadColumns = true )
    function setTable($table, $loadColumns = true)
    {
        $this->table = $table;
        if ($loadColumns) {
            $source = $this->getSource();
            if (isset($source)) {
                $this->columns = $this->getSource()->getColumns($this->table);
            } else {
                throw new RecessException('Data Source "' . $this->getSourceName() . '" is not set.', array());
            }
        } else {
            $this->columns = array();
        }
    }

Usage Example

 /** !Route POST, app/$app/model/gen */
 public function generateModel($app)
 {
     $values = $this->request->post;
     $modelName = $values['modelName'];
     $tableExists = $values['tableExists'] == 'yes' ? true : false;
     if ($tableExists) {
         $dataSource = $values['existingDataSource'];
         $createTable = false;
         $tableName = $values['existingTableName'];
     } else {
         $dataSource = $values['dataSource'];
         $createTable = $values['createTable'] == 'Yes' ? true : false;
         $tableName = $values['tableName'];
     }
     $propertyNames = $values['fields'];
     $primaryKey = $values['primaryKey'];
     $types = $values['types'];
     Library::import('recess.database.orm.Model', true);
     // Forcing b/c ModelDescriptor is in Model
     $modelDescriptor = new ModelDescriptor($modelName, false);
     $modelDescriptor->setSource($dataSource);
     $modelDescriptor->setTable($tableName, false);
     $pkFound = false;
     foreach ($propertyNames as $i => $name) {
         if ($name == "") {
             continue;
         }
         $property = new ModelProperty();
         $property->name = trim($name);
         if ($name == $primaryKey) {
             $property->isPrimaryKey = true;
         }
         if ($types[$i] == 'Integer Autoincrement') {
             if ($property->isPrimaryKey) {
                 $property->type = RecessType::INTEGER;
                 $property->isAutoIncrement = true;
             } else {
                 $property->type = RecessType::INTEGER;
             }
         } else {
             $property->type = $types[$i];
         }
         $modelDescriptor->properties[] = $property;
     }
     Library::import('recess.database.orm.ModelGen');
     $this->modelCode = ModelGen::toCode($modelDescriptor, $_ENV['dir.temp'] . 'Model.class.php');
     $app = new $app();
     if (strpos($app->modelsPrefix, 'recess.apps.') !== false) {
         $base = $_ENV['dir.recess'];
     } else {
         $base = $_ENV['dir.apps'];
     }
     $path = $base . str_replace(Library::dotSeparator, Library::pathSeparator, $app->modelsPrefix);
     $path .= $modelName . '.class.php';
     $this->path = $path;
     $this->modelWasSaved = false;
     $this->codeGenMessage = '';
     try {
         if (file_exists($this->path)) {
             if (file_get_contents($this->path) == $this->modelCode) {
                 $this->modelWasSaved = true;
             } else {
                 $this->codeGenMessage = 'File already exists!';
             }
         } else {
             file_put_contents($this->path, $this->modelCode);
             $this->modelWasSaved = true;
         }
     } catch (Exception $e) {
         $this->codeGenMessage = 'File could not be saved. Is models directory writeable?';
         $this->modelWasSaved = false;
     }
     $this->modelName = $modelName;
     $this->appName = get_class($app);
     $this->tableGenAttempted = $createTable;
     $this->tableWasCreated = false;
     $this->tableSql = '';
     if ($createTable) {
         $modelSource = Databases::getSource($dataSource);
         $this->tableSql = $modelSource->createTableSql($modelDescriptor);
         try {
             $modelSource->exec($this->tableSql);
             $this->tableWasCreated = true;
         } catch (Exception $e) {
             $this->tableWasCreated = false;
         }
     }
     return $this->ok('createModelComplete');
 }