Esempio n. 1
0
 /**
  * Save an individual record.
  * 
  * @param array $record The record to be saved
  * @param type $primaryKey The primary keys of the record
  * @return boolean
  */
 private function saveRecord(&$record, $primaryKey)
 {
     $status = ['success' => true, 'pk_assigned' => null, 'invalid_fields' => []];
     // Determine if the primary key of the record is set.
     $pkSet = $this->isPrimaryKeySet($primaryKey, $record);
     // Reset the data in the model to contain only the data to be saved
     $this->wrapper->setData($record);
     // Run preUpdate or preSave callbacks on models and behaviours
     if ($pkSet) {
         $this->wrapper->preUpdateCallback();
         $record = $this->wrapper->getData();
         $record = reset($record) === false ? [] : reset($record);
         $record = $this->runBehaviours('preUpdateCallback', [$record]);
     } else {
         $this->wrapper->preSaveCallback();
         $record = $this->wrapper->getData();
         $record = reset($record) === false ? [] : reset($record);
         $record = $this->runBehaviours('preSaveCallback', [$record]);
     }
     // Validate the data
     $validity = $this->validate($record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE);
     // Exit if data is invalid
     if ($validity !== true) {
         $status['invalid_fields'] = $validity;
         $status['success'] = false;
         return $status;
     }
     // Assign the data to the wrapper again
     $this->wrapper->setData($record);
     // Update or save the data and run post callbacks
     if ($pkSet) {
         $this->adapter->update($record);
         $this->wrapper->postUpdateCallback();
         $this->runBehaviours('postUpdateCallback', [$record]);
     } else {
         $this->adapter->insert($record);
         $keyValue = Db::getDriver()->getLastInsertId();
         $this->wrapper->{$primaryKey[0]} = $keyValue;
         $this->wrapper->postSaveCallback($keyValue);
         $this->runBehaviours('postSaveCallback', [$record, $keyValue]);
     }
     // Reset the data so it contains any modifications made by callbacks
     $record = $this->wrapper->getData()[0];
     return $status;
 }
Esempio n. 2
0
 /**
  * 
  * @param RecordWrapper $model
  */
 public function __construct($model)
 {
     $this->table = $model->getTable();
     $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName());
     $relationships = $model->getRelationships();
     $adapter = DriverAdapter::getDefaultInstance();
     $schema = Db::getDriver()->describeTable($this->table)[$this->table];
     $this->autoPrimaryKey = $schema['auto_increment'];
     foreach ($schema['columns'] as $field => $details) {
         $this->fields[$field] = ['type' => $adapter->mapDataTypes($details['type']), 'required' => !$details['nulls'], 'default' => $details['default'], 'name' => $field];
         if (isset($details['default'])) {
             $this->fields[$field]['default'] = $details['default'];
         }
         if (isset($details['length'])) {
             $this->fields[$field]['length'] = $details['length'];
         }
     }
     $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
     $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
     foreach ($relationships as $type => $relations) {
         $this->createRelationships($type, $relations);
     }
 }