|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Sofa\Eloquence\Subquery; |
|
6
|
|
|
use Illuminate\Database\Query\Builder as BaseBuilder; |
|
7
|
|
|
|
|
8
|
|
|
class Builder extends BaseBuilder |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Backup some fields for the pagination count. |
|
12
|
|
|
* |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
protected function backupFieldsForCount() |
|
16
|
|
|
{ |
|
17
|
|
|
foreach (['orders', 'limit', 'offset', 'columns'] as $field) { |
|
18
|
|
|
$this->backups[$field] = $this->{$field}; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$this->{$field} = null; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$bindings = ($this->from instanceof Subquery) ? ['order'] : ['order', 'select']; |
|
24
|
|
|
|
|
25
|
|
|
foreach ($bindings as $key) { |
|
26
|
|
|
$this->bindingBackups[$key] = $this->bindings[$key]; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$this->bindings[$key] = []; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Restore some fields after the pagination count. |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function restoreFieldsForCount() |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($this->backups as $field => $value) { |
|
40
|
|
|
$this->{$field} = $value; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
foreach ($this->bindingBackups as $key => $value) { |
|
|
|
|
|
|
44
|
|
|
$this->bindings[$key] = $value; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->backups = $this->bindingBackups = []; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Run a pagination count query. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $columns |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function runPaginationCountQuery($columns = ['*']) |
|
57
|
|
|
{ |
|
58
|
|
|
$bindings = $this->from instanceof Subquery ? ['order'] : ['select', 'order']; |
|
59
|
|
|
|
|
60
|
|
|
return $this->cloneWithout(['columns', 'orders', 'limit', 'offset']) |
|
61
|
|
|
->cloneWithoutBindings($bindings) |
|
62
|
|
|
->setAggregate('count', $this->withoutSelectAliases($columns)) |
|
63
|
|
|
->get()->all(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: