1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\ODM\Traits; |
9
|
|
|
|
10
|
|
|
use Spiral\ODM\Document; |
11
|
|
|
use Spiral\ODM\Entities\DocumentSelector; |
12
|
|
|
use Spiral\ODM\Entities\DocumentSource; |
13
|
|
|
use Spiral\ODM\ODM; |
14
|
|
|
use Spiral\ORM\Exceptions\ORMException; |
15
|
|
|
use Spiral\ORM\RecordEntity; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Static record functionality including create and find methods. |
19
|
|
|
*/ |
20
|
|
|
trait FindTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Find multiple records based on provided query. |
24
|
|
|
* |
25
|
|
|
* Example: |
26
|
|
|
* User::find(['status' => 'active'], ['profile']); |
27
|
|
|
* |
28
|
|
|
* @param array|\Closure $where Selection WHERE statement. |
29
|
|
|
* @return DocumentSelector |
30
|
|
|
*/ |
31
|
|
|
public static function find($where = []) |
32
|
|
|
{ |
33
|
|
|
return static::source()->find($where); |
|
|
|
|
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|\Closure $where Selection WHERE statement. |
44
|
|
|
* @param array $sortBy Sort by. |
45
|
|
|
* @return RecordEntity|null |
46
|
|
|
*/ |
47
|
|
|
public static function findOne($where = [], array $sortBy = []) |
48
|
|
|
{ |
49
|
|
|
return static::source()->findOne($where, $sortBy); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Find record using it's primary key. Relation data can be preloaded with found record. |
54
|
|
|
* |
55
|
|
|
* Example: |
56
|
|
|
* User::findByID(1, ['profile']); |
57
|
|
|
* |
58
|
|
|
* @param mixed $primaryKey Primary key. |
59
|
|
|
* @return Document|null |
60
|
|
|
*/ |
61
|
|
|
public static function findByPK($primaryKey) |
62
|
|
|
{ |
63
|
|
|
return static::source()->findByPK($primaryKey); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Instance of ORM Selector associated with specific document. |
68
|
|
|
* |
69
|
|
|
* @see Component::staticContainer() |
70
|
|
|
* @param ODM $odm ODM component, global container will be called if not instance provided. |
71
|
|
|
* @return DocumentSource |
72
|
|
|
* @throws ORMException |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public static function source(ODM $odm = null) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Using global container as fallback. |
78
|
|
|
* |
79
|
|
|
* @var ODM $odm |
80
|
|
|
*/ |
81
|
|
|
if (empty($odm)) { |
82
|
|
|
//Using global container as fallback |
83
|
|
|
$odm = self::staticContainer()->get(ODM::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $odm->source(static::class); |
87
|
|
|
} |
88
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.