|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Tylercd100\Database\Eloquent;
|
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder;
|
|
6
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
7
|
|
|
|
|
8
|
|
|
class Builder extends IlluminateBuilder
|
|
9
|
|
|
{
|
|
10
|
|
|
/**
|
|
11
|
|
|
* Find a model by its primary key.
|
|
12
|
|
|
*
|
|
13
|
|
|
* @param mixed $id
|
|
14
|
|
|
* @param array $columns
|
|
15
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
|
|
16
|
|
|
*/
|
|
17
|
|
|
public function find($id, $columns = ['*'])
|
|
18
|
|
|
{
|
|
19
|
|
|
if (! $this->isBasicQuery()) {
|
|
20
|
|
|
return parent::find($id, $columns);
|
|
|
|
|
|
|
21
|
|
|
}
|
|
22
|
|
|
|
|
23
|
|
|
if (is_array($id)) {
|
|
24
|
|
|
return $this->findMany($id, $columns);
|
|
25
|
|
|
}
|
|
26
|
|
|
|
|
27
|
|
|
return Cache::tags($this->model->getTable())->remember(
|
|
28
|
|
|
$id,
|
|
29
|
|
|
$this->model->cacheExpiry,
|
|
30
|
|
|
function () use ($id, $columns) {
|
|
31
|
|
|
return parent::find($id, $columns);
|
|
32
|
|
|
}
|
|
33
|
|
|
);
|
|
34
|
|
|
}
|
|
35
|
|
|
|
|
36
|
|
|
/**
|
|
37
|
|
|
* Find a model by its primary key.
|
|
38
|
|
|
*
|
|
39
|
|
|
* @param array $ids
|
|
40
|
|
|
* @param array $columns
|
|
41
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
42
|
|
|
*/
|
|
43
|
|
|
public function findMany($ids, $columns = ['*'])
|
|
44
|
|
|
{
|
|
45
|
|
|
if (! $this->isBasicQuery()) {
|
|
46
|
|
|
return parent::findMany($ids, $columns);
|
|
47
|
|
|
}
|
|
48
|
|
|
|
|
49
|
|
|
if (empty($ids)) {
|
|
50
|
|
|
return $this->model->newCollection();
|
|
51
|
|
|
}
|
|
52
|
|
|
|
|
53
|
|
|
return $this->model->newCollection(
|
|
54
|
|
|
Cache::tags($this->model->getTable())->rememberMany(
|
|
55
|
|
|
$ids,
|
|
56
|
|
|
$this->model->cacheExpiry,
|
|
57
|
|
|
function ($ids) use ($columns) {
|
|
58
|
|
|
return parent::findMany($ids, $columns)->all();
|
|
59
|
|
|
}
|
|
60
|
|
|
)
|
|
61
|
|
|
);
|
|
62
|
|
|
}
|
|
63
|
|
|
|
|
64
|
|
|
/**
|
|
65
|
|
|
* Check if the current query is a basic query or has had some
|
|
66
|
|
|
* conditions (where, joins, etc) attached to it.
|
|
67
|
|
|
*
|
|
68
|
|
|
* @return boolean
|
|
69
|
|
|
*/
|
|
70
|
|
|
protected function isBasicQuery()
|
|
71
|
|
|
{
|
|
72
|
|
|
$freshQuery = $this->query
|
|
73
|
|
|
->newQuery()
|
|
74
|
|
|
->from($this->model->getTable());
|
|
75
|
|
|
|
|
76
|
|
|
return $this->query == $freshQuery && empty($this->eagerLoad);
|
|
77
|
|
|
}
|
|
78
|
|
|
}
|
|
79
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.