Esempio n. 1
0
 /**
  * Register enjoin.
  */
 public function register()
 {
     $this->app->bind('enjoin', function () {
         Factory::bootstrap($this->options, $this->app);
         return new Enjoin();
     });
 }
Esempio n. 2
0
 /**
  * Records constructor.
  * @param Tree $Tree
  */
 public function __construct(Tree $Tree)
 {
     $this->Tree = $Tree;
     $Tree->walk(function (stdClass $node) {
         $node->getters = [];
         $skip = [];
         $Getters = Factory::getGetters();
         # Perform timestamps:
         if ($node->Model->isTimestamps()) {
             # Created at:
             $createdAtAttr = $node->Model->getCreatedAtAttr();
             if (in_array($createdAtAttr, $node->attributes)) {
                 $node->getters[$createdAtAttr] = $Getters->getCreatedAt($node->Model);
                 $skip[] = $createdAtAttr;
             }
             # Updated at:
             $updatedAtAttr = $node->Model->getUpdatedAtAttr();
             if (in_array($updatedAtAttr, $node->attributes)) {
                 $node->getters[$updatedAtAttr] = $Getters->getUpdatedAt($node->Model);
                 $skip[] = $updatedAtAttr;
             }
         }
         # Perform getters:
         $defAttributes = $node->Model->getDefinition()->getAttributes();
         foreach ($node->attributes as $attr) {
             if (!in_array($attr, $skip)) {
                 $node->getters[$attr] = $Getters->perform($node->Model, $defAttributes[$attr]);
             }
         }
     });
 }
Esempio n. 3
0
 /**
  * Tree constructor.
  * @param Model $Model
  * @param array $params
  */
 public function __construct(Model $Model, array $params)
 {
     $this->tree = $this->createNode($Model, $params);
     $this->hasChildren = count($this->tree->children) > 0;
     $this->hasLimit = isset($params['limit']);
     if ($this->hasChildren && Factory::getConfig()['enjoin']['auto_require']) {
         $this->walk(function (stdClass $node, array $path) {
             if (count($path) - 1) {
                 if ($node->where && is_null($node->required)) {
                     $node->required = true;
                 }
             }
         });
     }
 }
Esempio n. 4
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;
 }
Esempio n. 5
0
 /**
  * @param array $validate [ [attr, value, rules] ... ]
  * @throws \Enjoin\Exceptions\ValidationException
  */
 public function validate(array $validate)
 {
     $data = [];
     $rules = [];
     foreach ($validate as $it) {
         $data[$it[0]] = $it[1];
         $rules[$it[0]] = $it[2];
     }
     $validator = Factory::getValidator()->make($data, $rules);
     if ($validator->fails()) {
         $out = [];
         foreach ($validator->messages()->toArray() as $attr => $list) {
             $out[] = join(' ', $list);
         }
         Error::dropValidationException(join("\n", $out));
     }
 }
Esempio n. 6
0
 /**
  * @depends testModelFindOrCreate
  */
 public function testCache()
 {
     $this->handleDebug(__FUNCTION__);
     Factory::getCache()->flush();
     $it = Enjoin::get('Books')->findById(1, Enjoin::WITH_CACHE);
     $cache = Enjoin::get('Books')->cache()->getCacheInstance()->get('9125bfc211f5ddbce7352499c9c71973');
     $this->assertEquals($it, $cache);
 }
Esempio n. 7
0
 /**
  * @return array
  */
 public static function getQueryLog()
 {
     return Factory::getConnection()->getQueryLog();
 }
Esempio n. 8
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);
 }
Esempio n. 9
0
 /**
  * Flush cache.
  */
 public function flush()
 {
     if ($Cache = Factory::getCache()) {
         $tags = [];
         $this->getFlushTags($tags);
         if ($tags) {
             $Cache->tags($tags)->flush();
         }
     }
 }