Esempio n. 1
0
 private function pruneTags()
 {
     $all_tags = \App\Tag::withoutGlobalScopes()->get()->pluck('id')->toArray();
     $used_tags = DB::table('taggables')->distinct()->lists('tag_id');
     $unused_tags = array_diff($all_tags, $used_tags);
     $instance = new static();
     $key = $instance->getKeyName();
     foreach (\App\Tag::withoutGlobalScopes()->whereIn($key, $unused_tags)->get() as $tag) {
         $tag->delete();
     }
 }
Esempio n. 2
0
 /**
  * Main method, set source 
  * 
  * @param $source
  * @return static
  */
 public static function source($source)
 {
     $ins = new static();
     $ins->source = $ins->fixQuery($source);
     $ins->query = $ins->source;
     $ins->total_rows = $ins->getCount();
     $ins->key = $ins->getKeyName();
     //bind burp events
     BurpEvent::listen('dataset.sort', array($ins, 'sort'));
     BurpEvent::listen('dataset.page', array($ins, 'page'));
     return $ins;
 }
Esempio n. 3
0
 /**
  * Return an key-value array indicating the node's depth with $seperator.
  *
  * @return array
  */
 public static function getNestedList($column, $key = null, $seperator = ' ')
 {
     $instance = new static();
     $key = $key ?: $instance->getKeyName();
     $depthColumn = $instance->getDepthColumnName();
     $nodes = $instance->newNestedSetQuery()->visible()->get()->toArray();
     return array_combine(array_map(function ($node) use($key) {
         return $node[$key];
     }, $nodes), array_map(function ($node) use($seperator, $depthColumn, $column) {
         return str_repeat($seperator, $node[$depthColumn]) . $node[$column];
     }, $nodes));
 }
Esempio n. 4
0
 public function scopeMostVoted($query)
 {
     // not working
     //return $query->with(['voteCounter' => function($q){
     //    return $q->orderBy('up', 'DESC');
     //}]);
     $relatedClass = new static();
     $class = $relatedClass->getMorphClass();
     $table = $relatedClass->getTable();
     $primary = $relatedClass->getKeyName();
     $query->selectRaw("{$table}.*, voteable_id, voteable_type, IFNULL(up, 0), IFNULL(down, 0), IFNULL(up, 0) - IFNULL(down, 0) as total")->leftJoin('voteable_counter', 'voteable_counter.voteable_id', '=', "{$table}.{$primary}")->where(function ($queryWhere) use($class) {
         return $queryWhere->where('voteable_type', '=', $class)->orWhere('voteable_type', '=', null);
     })->orderBy('total', 'DESC')->orderBy('up', 'DESC');
 }
Esempio n. 5
0
 public static function destroy($ids)
 {
     $count = 0;
     $ids = is_array($ids) ? $ids : func_get_args();
     $instance = new static();
     $key = $instance->getKeyName();
     foreach ($instance->whereIn($key, $ids)->get() as $model) {
         if ($model->delete()) {
             $count++;
         }
     }
     return $count;
 }
Esempio n. 6
0
 /**
  * Destroy the models for the given IDs.
  *
  * @param  array|int  $ids
  * @return int
  */
 public static function destroy($ids)
 {
     // We'll initialize a count here so we will return the total number of deletes
     // for the operation. The developers can then check this number as a boolean
     // type value or get this total count of records deleted for logging, etc.
     $count = 0;
     $ids = is_array($ids) ? $ids : func_get_args();
     $instance = new static();
     // We will actually pull the models from the database table and call delete on
     // each of them individually so that their events get fired properly with a
     // correct set of attributes in case the developers wants to check these.
     $key = $instance->getKeyName();
     foreach ($instance->whereIn($key, $ids)->get() as $model) {
         if ($model->delete()) {
             $count++;
         }
     }
     return $count;
 }
Esempio n. 7
0
 /**
  * Destroy the models for the given IDs.
  *
  * @param  array|int  $ids
  * @return void
  */
 public static function destroy($ids)
 {
     $ids = is_array($ids) ? $ids : func_get_args();
     $ids = array_map(function ($value) {
         return $value instanceof MongoID ? $value : new MongoID($value);
     }, $ids);
     $instance = new static();
     // We will actually pull the models from the database table and call delete on
     // each of them individually so that their events get fired properly with a
     // correct set of attributes in case the developers wants to check these.
     $key = $instance->getKeyName();
     foreach ($instance->whereIn($key, $ids)->get() as $model) {
         $model->delete();
     }
 }
Esempio n. 8
0
 /**
  * Fixes the tree based on parentage info.
  *
  * Requires at least one root node. This will not update nodes with invalid parent.
  *
  * @return int The number of fixed nodes.
  */
 public static function fixTree()
 {
     $model = new static();
     $columns = [$model->getKeyName(), $model->getParentIdName(), $model->getLftName(), $model->getRgtName()];
     $nodes = $model->newQuery()->defaultOrder()->get($columns)->groupBy($model->getParentIdName());
     self::reorderNodes($nodes, $fixed);
     return $fixed;
 }
Esempio n. 9
0
 /**
  *  Function to check if slug exists
  *
  * @access  public
  * @param   string          $slugField
  * @param   string          $slug
  * @param   integer|string  $id
  * @return  string          $slug
  */
 public static function checkSlug($slugField, $slug, $id = null)
 {
     $instance = new static();
     $slug = $slug;
     $query = $instance->where($slugField, $slug);
     if ($id != null) {
         $query->whereNotIn($instance->getKeyName(), [$id]);
     }
     $nObjects = $query->count();
     if ($nObjects > 0) {
         $suffix = 0;
         while ($nObjects > 0) {
             $suffix++;
             $slug = $slug . '-' . $suffix;
             $nObjects = $instance->where($slugField, $slug)->count();
         }
     }
     return $slug;
 }
Esempio n. 10
0
 /**
  * Vérifie que la valeur de la clé primaire est présente dans la table
  * @param m $mCode Valeur de la clé primaire
  * @return boolean
  */
 public static function exists($mCode)
 {
     $oEmptyInstance = new static();
     $oEmptyInstance->getDB()->setQuery('select `' . $oEmptyInstance->getKeyName() . '` from `' . $oEmptyInstance->getTable() . '` where `' . $oEmptyInstance->getKeyName() . '` = "' . mysql_real_escape_string($mCode) . '"');
     return $oEmptyInstance->getDB()->getNumRows() > 0;
 }
Esempio n. 11
0
 /** We assume $modelCollection is the original model set, and $modelDataArray
  * are changes, if any. $modelDataArray should be an indexed array of 
  * associative arrays of fields to values. Any 'id'/key fields that are null
  * are assumed to be new, and created. Any id's present in $modelCollection 
  * but absent from $modelDataArray are assumed to be deleted, so the model is
  * deleted. When they have IDs in common, the models are updated with the
  * data from the array.
  * @param $modelCollection - EloquentCollection of PkModels
  * @param $modelDataArray array of new model data, probably from a POST
  */
 public static function updateModels($modelCollection, $modelDataArray)
 {
     $tstInstance = new static();
     $keyName = $tstInstance->getKeyName();
     $arrayKeys = [];
     foreach ($modelDataArray as $modelDataRow) {
         if (empty($modelDataRow[$keyName])) {
             #It's new, let's try to create it
             try {
                 static::create($modelDataRow);
             } catch (Exception $ex) {
                 pkdebug("Inserting Posted Data Row", $modelDataRow, 'Failed with Exception:', $ex);
             }
         } else {
             $arrayKeys[$modelDataRow[$keyName]] = $modelDataRow;
         }
     }
     foreach ($modelCollection as $model) {
         if (!in_array($model->{$keyName}, array_keys($arrayKeys))) {
             $model->delete();
         } else {
             $model->saveRelations($arrayKeys[$model->{$keyName}]);
         }
     }
 }
Esempio n. 12
0
 /**
  * Get query combining current model table with the specification table.
  * SELECT * FROM table1 JOIN specsTable ON table1.id = specsTable.table1_id
  *
  * @return mixed
  */
 public static function specsQuery()
 {
     $model = new static();
     $query = $model->newQuery();
     $specsTable = static::getSpecsTable();
     return $query->join($specsTable, "{$model->getTable()}.{$model->getKeyName()}", '=', "{$specsTable}.{$model->getForeignKey()}");
 }
 /**
  * Inner join with the translation table.
  *
  * @param \Illuminate\Database\Eloquent\Builder $query
  * @param null                                  $locale
  *
  * @return this
  */
 public static function scopeJoinTranslation(Builder $query, $locale = null)
 {
     $instance = new static();
     $translationModelName = $instance->getTranslationModelName();
     $translationModel = new $translationModelName();
     if (is_null($locale)) {
         $locale = $instance->locale();
     }
     return $query->join($translationModel->getTable() . ' as t', 't.' . $instance->getRelationKey(), '=', $instance->getTable() . '.' . $instance->getKeyName())->where($instance->getLocaleKey(), $locale);
 }