Example #1
0
 /**
  * @return array
  */
 public function getPrepared()
 {
     $e = $this->Model->dialectify()->getEscapeChar();
     $table = $this->Model->getTableName();
     list($query, $place) = (new Where($this->Model, $this->where))->getPrepared();
     $query = "DELETE FROM {$e}{$table}{$e} WHERE {$query}";
     return [$query, $place];
 }
Example #2
0
 /**
  * @return array
  */
 public function getPrepared()
 {
     $e = $this->Model->dialectify()->getEscapeChar();
     $table = $this->Model->getTableName();
     list($setQuery, $setPlace) = $this->handleSet($e);
     $query = "UPDATE {$e}{$table}{$e} SET {$setQuery}";
     $wherePlace = [];
     if ($this->where) {
         list($whereQuery, $wherePlace) = (new Where($this->Model, $this->where))->getPrepared();
         $query .= " WHERE {$whereQuery}";
     }
     return [$query, array_merge($setPlace, $wherePlace)];
 }
Example #3
0
 /**
  * @param string $field
  * @return string
  */
 private function getTableField($field)
 {
     $esc = $this->Model->dialectify()->getEscapeChar();
     $out = $esc . $field . $esc;
     if ($this->prefix) {
         $out = $esc . $this->prefix . $esc . '.' . $out;
     }
     return $out;
 }
Example #4
0
 /**
  * @param string $str
  * @return string
  */
 protected function handleColumnedString($str)
 {
     $e = $this->Model->dialectify()->getEscapeChar();
     $arr = explode('.', $str);
     if (count($arr) > 1) {
         $last = array_pop($arr);
         $str = join('.', $arr);
         return "{$e}{$str}{$e}.{$e}{$last}{$e}";
     }
     return "{$e}{$str}{$e}";
 }
Example #5
0
 /**
  * @param array $tags
  * @return null
  */
 public function getFlushTags(array &$tags)
 {
     $unique = $this->Model->getUnique();
     if (in_array($unique, $tags)) {
         return null;
     }
     $tags[] = $unique;
     foreach ($this->Model->getDefinition()->getRelations() as $relation) {
         $relation->Model->cache()->getFlushTags($tags);
     }
 }
Example #6
0
 /**
  * @param string $type
  * @param Model $Model
  * @param array $options
  * @return stdClass
  */
 private static function performRelation($type, $Model, array $options = [])
 {
     $as = isset($options['as']) ? $options['as'] : null;
     if (array_key_exists('foreignKey', $options)) {
         $foreignKey = $options['foreignKey'];
     } else {
         $className = get_class($Model->getDefinition());
         if ($pos = strrpos($className, '\\')) {
             $className = substr($className, $pos + 1);
         }
         $foreignKey = Inflector::tableize($className) . '_id';
     }
     $relatedKey = $Model->getUnique();
     !$as ?: ($relatedKey .= Extras::GLUE_CHAR . $as);
     $relation = new stdClass();
     $relation->Model = $Model;
     // required for cache
     $relation->type = $type;
     $relation->as = $as;
     $relation->foreignKey = $foreignKey;
     $relation->relatedKey = $relatedKey;
     return $relation;
 }
Example #7
0
 /**
  * Defines: Model, key, prefix, as, asProp, skip, attributes, where, required, children.
  * @param Model $Model
  * @param array $params
  * @param null|stdClass $parent
  * @return stdClass
  */
 private function createNode(Model $Model, array $params = [], stdClass $parent = null)
 {
     $node = new stdClass();
     $node->Model = $Model;
     $node->key = $Model->getUnique();
     $node->prefix = null;
     $node->asProp = null;
     # Handle `as`:
     $node->as = null;
     if (isset($params['as'])) {
         $node->as = $params['as'];
         $node->key .= Extras::GLUE_CHAR . $params['as'];
     }
     # Handle `attributes`:
     $node->skip = [];
     $attrs = array_keys($Model->getDefinition()->getAttributes());
     # Handle timestamps:
     if ($Model->isTimestamps()) {
         $attrs[] = $Model->getCreatedAtAttr();
         $attrs[] = $Model->getUpdatedAtAttr();
     }
     # Handle attributes in options:
     if (isset($params['attributes']) && $params['attributes']) {
         $attrs = array_intersect($attrs, $params['attributes']);
     }
     if (!in_array('id', $attrs)) {
         array_unshift($attrs, 'id');
         $node->skip[] = 'id';
     }
     $node->attributes = $attrs;
     # Handle `where`:
     $node->where = isset($params['where']) ? $params['where'] : [];
     # Handle `required`:
     $node->required = isset($params['required']) ? (bool) $params['required'] : null;
     # Handle `include`:
     !$parent ?: $this->handleRelation($parent, $node);
     $this->handleChildren($node, $params);
     return $node;
 }
Example #8
0
 /**
  * @param Model $Model
  * @return Closure
  */
 private function getDate(Model $Model)
 {
     $dateFormat = $Model->dialectify()->getDateFormat();
     return function ($attr, Closure $getValue) use($dateFormat) {
         $value = $getValue($attr);
         return is_string($value) ? Carbon::createFromFormat($dateFormat, $value) : $value;
     };
 }
Example #9
0
 /**
  * Handle date/datetime.
  * @param Model $Model
  * @param mixed $value
  * @return mixed
  */
 private function getDate(Model $Model, $value)
 {
     if ($value instanceof Carbon) {
         return $value->format($Model->dialectify()->getDateFormat());
     }
     return $value;
 }
Example #10
0
 /**
  * @param \Enjoin\Model\Model $Model
  * @param string $type
  * @param null|int $id
  */
 public function __construct(Model $Model, $type = Engine::NON_PERSISTENT, $id = null)
 {
     $this->Scope = new Scope($Model->getName(), $type, $id);
 }