Пример #1
0
 public function __construct()
 {
     parent::__construct();
     if (!UUID::is_valid($this->key)) {
         $this->key = UUID::v4();
     }
 }
 public function save($automatic_reload = true)
 {
     if (!$this->created) {
         $this->created = date("Y-m-d H:i:s");
     }
     parent::save($automatic_reload);
 }
Пример #3
0
 public function save($automatic_reload = true)
 {
     $databaseLayer = DatabaseLayer::getInstance();
     $lockController = $databaseLayer->lockController($this->getTable(), $this->getTableAlias());
     // Check the table exists.
     $this->getTableBuilder()->build();
     // Lock the table.
     $lockController->lock();
     // Get our primary key
     $primaryColumn = $this->getIDField();
     // Get the highest primary key
     if (!$this->{$primaryColumn}) {
         $highest = DumbModel::query("SELECT max({$primaryColumn}) as highest FROM {$this->getTable()}");
         $highestKey = end($highest)->highest;
         // Set our primary key to this +1
         $newKey = isset($highestKey) ? $highestKey + 1 : 1;
         $this->{$primaryColumn} = $newKey;
         // echo "{$this->get_table()}: {$primaryColumn} = {$newKey}\n";
     }
     // Set sequence to sequence + 1
     $highestSequence = DumbModel::query("SELECT max(sequence) as highest FROM {$this->getTable()} WHERE `{$primaryColumn}` = '{$this->{$primaryColumn}}'");
     $highestSequenceKey = end($highestSequence)->highest;
     if (!$highestSequenceKey) {
         $this->sequence = 1;
     } else {
         $this->sequence = intval($highestSequenceKey) + 1;
     }
     // Save the object
     parent::save($automatic_reload);
     // Unlock the table.
     $lockController->unlock();
     // return the object.
     return $this;
 }
Пример #4
0
 public function __toArray($anticipated_rows = null)
 {
     $array = parent::__toArray($anticipated_rows);
     $array['Application'] = $this->get_application()->__toArray();
     return $array;
 }
Пример #5
0
 public function buildTable(ActiveRecord $model)
 {
     $schema = $model->getClassSchema();
     $params = array();
     foreach ($model->__calculateSaveDownRows() as $p => $parameter) {
         $auto_increment = false;
         $type = "varchar(200)";
         $auto_increment_possible = false;
         if (isset($schema[$parameter])) {
             $psuedo_type = $schema[$parameter]['type'];
             switch (strtolower($psuedo_type)) {
                 case 'int':
                 case 'integer':
                     $length = isset($schema[$parameter]['length']) ? $schema[$parameter]['length'] : 10;
                     $type = "INT";
                     $auto_increment_possible = true;
                     break;
                 case 'string':
                     $length = isset($schema[$parameter]['length']) ? $schema[$parameter]['length'] : 200;
                     $type = "VARCHAR({$length})";
                     break;
                 case 'date':
                 case 'datetime':
                     $type = 'TIME';
                     break;
                 case 'enum':
                     $type = "ENUM('" . implode("', '", $schema[$parameter]['options']) . "')";
                     break;
                 case 'text':
                     $type = "TEXT";
                     break;
                 case 'blob':
                     $type = 'BLOB';
                     break;
                 case "decimal":
                     $type = "DECIMAL(" . implode(",", $schema[$parameter]['options']) . ")";
                     break;
                 case "uuid":
                     $type = "VARCHAR(" . strlen(UUID::v4()) . ")";
                     break;
                 case "md5":
                     $type = "VARCHAR(" . strlen(md5("test")) . ")";
                     break;
                 case "sha1":
                     $type = "VARCHAR(" . strlen(sha1("test")) . ")";
                     break;
             }
         }
         if ($p == 0) {
             // First param always primary key if possible
             if ($auto_increment_possible) {
                 $primary_key = $parameter;
                 if (!$model instanceof VersionedActiveRecord) {
                     $auto_increment = true;
                 }
             }
         }
         if ($auto_increment) {
             $type = 'SERIAL';
         }
         $nullability = $schema[$parameter]['nullable'] ? "NULL" : "NOT NULL";
         $params[] = "  " . trim("\"{$parameter}\" {$type} {$nullability}");
     }
     // Disable auto-increment if this object is versioned.
     if ($model instanceof VersionedActiveRecord) {
         if (isset($primary_key)) {
             $params[] = "  PRIMARY KEY (\"{$primary_key}\", \"sequence\")";
         }
     } else {
         if (isset($primary_key)) {
             $params[] = "  PRIMARY KEY (\"{$primary_key}\")";
         }
     }
     $query = "CREATE TABLE IF NOT EXISTS \"{$model->getTableName()}\"\n";
     $query .= "(\n";
     $query .= implode(",\n", $params) . "\n";
     $query .= ");\n";
     // Log it.
     if (DatabaseLayer::getInstance()->getLogger() instanceof Logger) {
         DatabaseLayer::getInstance()->getLogger()->addInfo("Creating table {$model->getTableName()}\n\n{$query}");
     }
     $this->query($query);
 }
Пример #6
0
 public function buildTable(ActiveRecord $model)
 {
     $schema = $model->getClassSchema();
     $params = array();
     foreach ($model->__calculateSaveDownRows() as $p => $parameter) {
         $auto_increment = false;
         $type = "varchar(200)";
         $auto_increment_possible = false;
         if (isset($schema[$parameter])) {
             $psuedo_type = $schema[$parameter]['type'];
             switch (strtolower($psuedo_type)) {
                 case 'int':
                 case 'integer':
                     $type = "INTEGER";
                     $auto_increment_possible = true;
                     break;
                 case 'date':
                 case 'datetime':
                 case 'enum':
                 case 'string':
                 case 'text':
                 case 'uuid':
                 case 'md5':
                 case 'sha1':
                     $type = "TEXT";
                     break;
                 case 'blob':
                     $type = 'BLOB';
                     break;
             }
         }
         $is_primary_key = false;
         if ($p == 0) {
             // First param always primary key if possible
             if ($auto_increment_possible) {
                 $is_primary_key = "PRIMARY KEY";
                 if (!$model instanceof VersionedActiveRecord) {
                     $auto_increment = true;
                 }
             }
         }
         if ($auto_increment && !$model instanceof VersionedActiveRecord) {
             $auto_increment_sql = 'AUTOINCREMENT';
         } else {
             $auto_increment_sql = false;
         }
         $nullability = $schema[$parameter]['nullable'] ? "NULL" : "NOT NULL";
         $nullability = $is_primary_key ? '' : $nullability;
         $is_primary_key = !$model instanceof VersionedActiveRecord ? $is_primary_key : null;
         $params[] = "  " . trim("`{$parameter}` {$type} {$is_primary_key} {$auto_increment_sql} {$nullability}");
     }
     $query = "CREATE TABLE IF NOT EXISTS `{$model->getTableName()}`\n";
     $query .= "(\n";
     $query .= implode(",\n", $params) . "\n";
     $query .= ")\n";
     $this->query($query);
     // Log it.
     if (DatabaseLayer::getInstance()->getLogger() instanceof Logger) {
         DatabaseLayer::getInstance()->getLogger()->addInfo("Creating table {$model->getTableName()}\n\n{$query}");
     }
 }