Skip to content

Add belongs-to-many caching functionality. #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class CacheKey

public function __construct(
array $eagerLoad,
Model $model,
Builder $query
$model,
$query
) {
$this->eagerLoad = $eagerLoad;
$this->model = $model;
Expand Down Expand Up @@ -54,7 +54,9 @@ protected function getIdColumn(string $idColumn) : string

protected function getLimitClause() : string
{
if (! $this->query->limit) {
if (! property_exists($this->query, "limit")
|| ! $this->query->limit
) {
return "";
}

Expand All @@ -74,7 +76,9 @@ protected function getModelSlug() : string

protected function getOffsetClause() : string
{
if (! $this->query->offset) {
if (! property_exists($this->query, "offset")
|| ! $this->query->offset
) {
return "";
}

Expand All @@ -83,6 +87,12 @@ protected function getOffsetClause() : string

protected function getOrderByClauses() : string
{
if (! property_exists($this->query, "orders")
|| ! $this->query->orders
) {
return "";
}

$orders = collect($this->query->orders);

return $orders
Expand All @@ -100,12 +110,15 @@ protected function getQueryColumns(array $columns) : string
{
if (($columns === ["*"]
|| $columns === [])
&& ! $this->query->columns
&& (! property_exists($this->query, "columns")
|| ! $this->query->columns)
) {
return "";
}

if ($this->query->columns) {
if (property_exists($this->query, "columns")
&& $this->query->columns
) {
return "_" . implode("_", $this->query->columns);
}

Expand Down Expand Up @@ -302,7 +315,9 @@ protected function getWheres(array $wheres) : Collection
{
$wheres = collect($wheres);

if ($wheres->isEmpty()) {
if ($wheres->isEmpty()
&& property_exists($this->query, "wheres")
) {
$wheres = collect($this->query->wheres);
}

Expand Down
4 changes: 2 additions & 2 deletions src/CacheTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class CacheTags

public function __construct(
array $eagerLoad,
Model $model,
Builder $query
$model,
$query
) {
$this->eagerLoad = $eagerLoad;
$this->model = $model;
Expand Down
15 changes: 15 additions & 0 deletions src/CachedBelongsToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use Fico7489\Laravel\Pivot\Traits\FiresPivotEventsTrait;
use GeneaLabs\LaravelModelCaching\Traits\Buildable;
use GeneaLabs\LaravelModelCaching\Traits\BuilderCaching;
use GeneaLabs\LaravelModelCaching\Traits\Caching;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class CachedBelongsToMany extends BelongsToMany
{
use Buildable;
use BuilderCaching;
use Caching;
use FiresPivotEventsTrait;
}
Loading