示例#1
0
 /**
  * Inserts one table entry and, optionally, fills the columns with information from 
  * the database or a provided array.
  * Returns the primary key of the new row.
  * @param array $data row data
  * @throws Exception
  * @return int $id
  */
 public function insertRow(array $data = array())
 {
     if (empty($data)) {
         throw new Exception('$data not provided in ' . get_class($this) . '::' . __FUNCTION__ . '()');
     }
     if (isset($data['autofill'])) {
         $autofill = $data['autofill'];
         unset($data['autofill']);
     }
     if (isset($data['tableDescription'])) {
         $tableDescription = $data['tableDescription'];
         unset($data['tableDescription']);
     }
     // store row in database and get id
     $this->getAdapter()->insert('Data_Tables', $data);
     $id = $this->getAdapter()->lastInsertId();
     if (isset($autofill) && !empty($autofill)) {
         // get the additional resources
         $columnResource = new Data_Model_Resource_Columns();
         $databaseResource = new Data_Model_Resource_Databases();
         // auto create entries for all columns
         $row = $databaseResource->fetchRow($data['database_id']);
         $database = $row['name'];
         $table = $data['name'];
         try {
             if (empty($tableDescription)) {
                 $descResource = new Data_Model_Resource_Description();
                 $descResource->init($database);
                 $tableDescription = $descResource->describeTable($table);
             }
             foreach ($tableDescription['columns'] as $column) {
                 $column['table'] = $table;
                 $column['table_id'] = $id;
                 $column['database'] = $database;
                 $columnResource->insertRow($column);
             }
         } catch (Exception $e) {
             $this->getAdapter()->delete('Data_Tables', array('`id` = ?' => $id));
             throw $e;
         }
     }
     return $id;
 }