|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Spiral\ORM\Traits; |
|
9
|
|
|
|
|
10
|
|
|
use Spiral\ORM\Entities\RecordSelector; |
|
11
|
|
|
use Spiral\ORM\Entities\RecordSource; |
|
12
|
|
|
use Spiral\ORM\Exceptions\ORMException; |
|
13
|
|
|
use Spiral\ORM\ORM; |
|
14
|
|
|
use Spiral\ORM\RecordEntity; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Static record functionality including create and find methods. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait FindTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Find multiple records based on provided query. |
|
23
|
|
|
* |
|
24
|
|
|
* Example: |
|
25
|
|
|
* User::find(['status' => 'active'], ['profile']); |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $where Selection WHERE statement. |
|
28
|
|
|
* @param array $load Array or relations to be pre-loaded. |
|
29
|
|
|
* @return RecordSelector |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function find($where = [], array $load = []) |
|
32
|
|
|
{ |
|
33
|
|
|
return static::source()->find($where)->load($load); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Fetch one record based on provided query or return null. Use second argument to specify |
|
38
|
|
|
* relations to be loaded. |
|
39
|
|
|
* |
|
40
|
|
|
* Example: |
|
41
|
|
|
* User::findOne(['name' => 'Wolfy-J'], ['profile'], ['id' => 'DESC']); |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $where Selection WHERE statement. |
|
44
|
|
|
* @param array $load Array or relations to be pre-loaded. |
|
45
|
|
|
* @param array $orderBy Sort by conditions. |
|
46
|
|
|
* @return RecordEntity|null |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function findOne($where = [], array $load = [], array $orderBy = []) |
|
49
|
|
|
{ |
|
50
|
|
|
$source = static::find($where, $load); |
|
51
|
|
|
foreach ($orderBy as $column => $direction) { |
|
52
|
|
|
$source->orderBy($column, $direction); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $source->findOne(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Find record using it's primary key. Relation data can be preloaded with found record. |
|
60
|
|
|
* |
|
61
|
|
|
* Example: |
|
62
|
|
|
* User::findByID(1, ['profile']); |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $primaryKey Primary key. |
|
65
|
|
|
* @param array $load Array or relations to be pre-loaded. |
|
66
|
|
|
* @return RecordEntity|null |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function findByPK($primaryKey, array $load = []) |
|
69
|
|
|
{ |
|
70
|
|
|
return static::source()->find()->load($load)->findByPK($primaryKey); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Instance of ORM Selector associated with specific document. |
|
75
|
|
|
* |
|
76
|
|
|
* @see Component::staticContainer() |
|
77
|
|
|
* @param ORM $orm ORM component, global container will be called if not instance provided. |
|
78
|
|
|
* @return RecordSource |
|
79
|
|
|
* @throws ORMException |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public static function source(ORM $orm = null) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
/** |
|
84
|
|
|
* Using global container as fallback. |
|
85
|
|
|
* |
|
86
|
|
|
* @var ORM $orm |
|
87
|
|
|
*/ |
|
88
|
|
|
if (empty($orm)) { |
|
89
|
|
|
//Using global container as fallback |
|
90
|
|
|
$orm = self::staticContainer()->get(ORM::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $orm->source(static::class); |
|
94
|
|
|
} |
|
95
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: