1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ronanchilvers\Orm; |
4
|
|
|
|
5
|
|
|
use Ronanchilvers\Orm\Orm; |
6
|
|
|
use Ronanchilvers\Orm\QueryBuilder; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base finder class for retrieving entities |
11
|
|
|
* |
12
|
|
|
* @method \PDO getConnection() |
13
|
|
|
* @method array all(int $page = null, int $perPage = 10) |
14
|
|
|
* @method \Ronanchilvers\Orm\Model|null first() |
15
|
|
|
* @method \Ronanchilvers\Orm\Model|null one(int $id) |
16
|
|
|
* @method \ClanCats\Hydrahon\Query\Sql\Select select() |
17
|
|
|
* @method mixed query(string $sql, array $params = [], int $page = null, int $perPage = 20) |
18
|
|
|
* @method \ClanCats\Hydrahon\Query\Sql\Insert insert() |
19
|
|
|
* @method \ClanCats\Hydrahon\Query\Sql\Update update() |
20
|
|
|
* @method \ClanCats\Hydrahon\Query\Sql\Delete delete() |
21
|
|
|
* @author Ronan Chilvers <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Finder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $modelClass; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class constructor |
32
|
|
|
* |
33
|
|
|
* @author Ronan Chilvers <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
public function __construct(string $modelClass) |
36
|
|
|
{ |
37
|
|
|
$this->modelClass = $modelClass; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Magic call for instance methods |
42
|
|
|
* |
43
|
|
|
* This method maps method calls onto the query builder |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
* @author Ronan Chilvers <[email protected]> |
47
|
|
|
*/ |
48
|
|
|
public function __call($method, $args) |
49
|
|
|
{ |
50
|
|
|
$builder = $this->newQueryBuilder(); |
51
|
|
|
if (method_exists($builder, $method)) { |
52
|
|
|
return call_user_func_array([$builder, $method], $args); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw new RuntimeException( |
56
|
|
|
sprintf( |
57
|
|
|
'Undefined method %s::%s()', |
58
|
|
|
get_called_class(), |
59
|
|
|
$method |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a new query builder for this finder |
66
|
|
|
* |
67
|
|
|
* @return \Ronanchilvers\Orm\QueryBuilder |
68
|
|
|
* @author Ronan Chilvers <[email protected]> |
69
|
|
|
*/ |
70
|
|
|
protected function newQueryBuilder() |
71
|
|
|
{ |
72
|
|
|
return new QueryBuilder( |
73
|
|
|
Orm::getConnection(), |
74
|
|
|
$this->modelClass |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|