/** * Convert a Jasny DB styled filter to a MongoDB query. * * @param array $filter * @param array $opts * @return array */ protected static function filterToQuery($filter, array $opts = []) { $filter = static::castForDB($filter); $filter = static::mapToFields($filter); if (in_array('from-trash', $opts)) { $filter['_deleted'] = true; } elseif (!in_array('include-trash', $opts)) { $filter['_deleted'] = null; } return DB::filterToQuery($filter); }
/** * Convert a Jasny DB styled filter to a MongoDB query. * * @param array $filter * @param array $opts * @return array */ protected static function filterToQuery($filter, array $opts = []) { $filter = static::castForDB($filter); $filter = static::mapToFields($filter); $opts += ['deleted' => false]; // Defaults to no deleted if ($opts['deleted'] === 'only' || in_array('from-trash', $opts, true)) { $filter['_deleted'] = true; } elseif ($opts['deleted'] !== 'included' && !in_array('include-trash', $opts, true)) { $filter['_deleted'] = null; } return DB::filterToQuery($filter); }
/** * Fetch all documents. * * @param array $filter * @param array $sort * @param int|array $limit Limit or [limit, offset] * @param array $opts * @return EntitySet|static[]|\Jasny\DB\Entity[] */ public static function fetchAll(array $filter = [], $sort = [], $limit = null, array $opts = []) { $collection = static::getCollection(); // Query $query = static::filterToQuery($filter, $opts); $cursor = $collection->find($query); $totalFn = function () use($collection, $query) { return $collection->count($query); }; // Sort if (is_a(get_called_class(), Sorted::class, true)) { $sort = (array) $sort + static::getDefaultSorting(); } $querySort = DB::sortToQuery($sort); if (!empty($querySort)) { $cursor->sort($querySort); } // Limit / skip list($limit, $skip) = (array) $limit + [null, null]; if (isset($limit)) { $cursor->limit($limit); } if (isset($skip)) { $cursor->skip($skip); } // Return $class = static::getDocumentClass(); return $class::entitySet($cursor, $totalFn); }