/** * Returns the cursor for the referenced documents as objects * * @param Model $model * @param string $field * @param bool $cachable * * @return array */ protected function referencesMany($model, $field, $cachable = true) { $ref_ids = $this->{$field}; if (!isset($ref_ids[0])) { return []; } if ($this->isMongoId($ref_ids[0])) { foreach ($ref_ids as $key => $value) { $ref_ids[$key] = new MongoId($value); } } if ($cachable && static::$cacheComponent) { $cache_key = 'reference_cache_' . $model . '_' . md5(serialize($ref_ids)); // For the next 6 seconds (0.1 minute), the last retrived value // will be returned from cache =) return static::$cacheComponent->remember($cache_key, 0.1, function () use($model, $ref_ids) { return $model::where(['_id' => ['$in' => $ref_ids]], [], true); }); } elseif ($cachable) { return $model::where(['_id' => ['$in' => $ref_ids]], [], true); } else { return $model::where(['_id' => ['$in' => $ref_ids]]); } }