示例#1
0
 /**
  * @param \Enjoin\Record\Record $Record
  * @param array|null $params
  * @param int $flags
  * @return \Enjoin\Record\Record
  */
 public static function save(Record $Record, array $params = null, $flags = 0)
 {
     $scope = $Record->scope();
     $Model = Enjoin::get($scope->modelName);
     if ($Model->isTimestamps()) {
         $createdAtField = $Model->getCreatedAtField();
         if ($scope->type === self::NON_PERSISTENT || $scope->type === self::PERSISTENT && !isset($Record->{$createdAtField})) {
             $Record->{$createdAtField} = Carbon::now();
         }
         $updatedAtField = $Model->getUpdatedAtField();
         $Record->{$updatedAtField} = Carbon::now();
     }
     $defAttributes = $Model->getDefinition()->getAttributes();
     $pick = isset($params['fields']) ? $params['fields'] : null;
     $volume = [];
     $validate = [];
     $record = $Record->__toArray();
     $Setters = Factory::getSetters();
     foreach ($Record as $field => $recordVal) {
         if ($recordVal instanceof Record) {
             # We can start recursive saving here...
             #$recordVal->save();
         } elseif (array_key_exists($field, $defAttributes)) {
             $saveVal = $Setters->perform($Model, $record, $defAttributes[$field], $field);
             if (isset($defAttributes[$field]['validate'])) {
                 $validate[] = [$field, $saveVal, $defAttributes[$field]['validate']];
             }
             if (!$pick || in_array($field, $pick)) {
                 $volume[$field] = $saveVal;
             }
         } elseif ($Model->isTimestamps() && $field === $createdAtField) {
             if (!$pick || in_array($field, $pick)) {
                 $volume[$field] = $Setters->getCreatedAt($Model, $recordVal);
             }
         } elseif ($Model->isTimestamps() && $field === $updatedAtField) {
             $volume[$field] = $Setters->getUpdatedAt($Model, $recordVal);
         }
     }
     !$validate ?: $Setters->validate($validate);
     if (!($flags & self::SOFT_SAVE)) {
         //            $id = $this->saveEntry($volume);
         $id = $scope->type === self::NON_PERSISTENT ? static::saveNonPersistent($Record, $volume) : static::savePersistent($Record, $volume);
         $scope->id = $id;
         $Record->id = $id;
     }
     return $Record;
 }
示例#2
0
 /**
  * @todo: Add second argument: array|null $params (see http://docs.sequelizejs.com/en/v3/api/model/).
  * @param array $collections
  * @return bool
  */
 public function bulkCreate(array $collections)
 {
     $bulk = [];
     $Setters = Factory::getSetters();
     $defAttributes = $this->getDefinition()->getAttributes();
     foreach ($collections as $record) {
         $volume = [];
         $skip = [];
         # Perform timestamps:
         if ($this->isTimestamps()) {
             # Created at:
             $createdAtField = $this->getCreatedAtField();
             $volume[$createdAtField] = $Setters->getCreatedAt($this, isset($record[$createdAtField]) ? $record[$createdAtField] : null);
             $skip[] = $createdAtField;
             # Updated at:
             $updatedAtField = $this->getUpdatedAtField();
             $volume[$updatedAtField] = $Setters->getUpdatedAt($this);
             $skip[] = $updatedAtField;
         }
         # Perform setters:
         $validate = [];
         foreach (array_diff(array_keys($record), $skip) as $attr) {
             if (array_key_exists($attr, $defAttributes)) {
                 $volume[$attr] = $Setters->perform($this, $record, $defAttributes[$attr], $attr);
                 if (isset($defAttributes[$attr]['validate'])) {
                     $validate[] = [$attr, $volume[$attr], $defAttributes[$attr]['validate']];
                 }
             }
         }
         !$validate ?: $Setters->validate($validate);
         $bulk[] = $volume;
     }
     $this->cache()->flush();
     return $this->queryBuilder()->insert($bulk);
 }