Skip to content

Commit

Permalink
avoid extra sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 26, 2015
1 parent 74a9689 commit 66e128b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Conversion/ConversionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ public function getByName($name)
*/
protected function addConversionsFromRelatedModel(Media $media)
{
static $cachedRegistrations = [];

$cacheKey = $media->model_type;

if (! isset($cachedRegistrations[$cacheKey])) {

$cachedRegistrations[$cacheKey] = [];

if ($media->model instanceof HasMedia) {
$media->model->registerMediaConversions();
$cachedRegistrations[$cacheKey] = $media->model->mediaConversions;
}
$modelName = $media->model_type;

/*
* To prevent an sql query create a new model instead
* of the using the associated one
*/
$model = new $modelName();

if ($model instanceof HasMedia) {
/*
* TODO: make this method static in the next version
*/
$model->registerMediaConversions();
}

$this->items = $cachedRegistrations[$cacheKey];
$this->items = $model->mediaConversions;
}

/**
Expand Down

0 comments on commit 66e128b

Please sign in to comment.