1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 08.12.15 |
6
|
|
|
* Time: 22:14 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api\query; |
9
|
|
|
|
10
|
|
|
use samsonframework\orm\QueryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generic SamsonCMS query for retrieving entities that have relation between each other |
14
|
|
|
* through other relational entity. |
15
|
|
|
* |
16
|
|
|
* @package samsoncms\api |
17
|
|
|
*/ |
18
|
|
|
class Base |
19
|
|
|
{ |
20
|
|
|
/** Deletion flag field name */ |
21
|
|
|
const DELETE_FLAG_FIELD = 'Active'; |
22
|
|
|
|
23
|
|
|
/** @var QueryInterface Database query instance */ |
24
|
|
|
protected $query; |
25
|
|
|
|
26
|
|
|
/** @var string Entity identifier */ |
27
|
|
|
protected $identifier; |
28
|
|
|
|
29
|
|
|
/** @var string Entity primary field name */ |
30
|
|
|
protected $primaryField; |
31
|
|
|
|
32
|
|
|
/** @var string Related entity primary field */ |
33
|
|
|
protected $relationPrimary; |
34
|
|
|
|
35
|
|
|
/** @var string Relation entity identifier */ |
36
|
|
|
protected $relationIdentifier; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Entity constructor. |
40
|
|
|
* @param QueryInterface $query Database query instance |
41
|
|
|
* @param string $identifier Entity identifier |
42
|
|
|
* @param string $relationPrimary Relation entity primary field name |
43
|
|
|
* @param string $relationIdentifier Relation entity identifier |
44
|
|
|
*/ |
45
|
|
|
public function __construct(QueryInterface $query, $identifier, $relationPrimary, $relationIdentifier) |
46
|
|
|
{ |
47
|
|
|
$this->query = $query; |
48
|
|
|
$this->identifier = $identifier; |
49
|
|
|
$this->primaryField = $identifier::$_primary; |
50
|
|
|
$this->relationPrimary = $relationPrimary; |
51
|
|
|
$this->relationIdentifier = $relationIdentifier; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get current entity identifiers collection by navigation identifier. |
56
|
|
|
* |
57
|
|
|
* @param string $relationID Relation entity identifier |
58
|
|
|
* @param mixed $relationValue Relation entity value |
59
|
|
|
* @param array $filteringIDs Collection of entity identifiers for filtering query |
60
|
|
|
* @return array Collection of entity identifiers filtered by navigation identifier. |
61
|
|
|
*/ |
62
|
|
|
public function idsByRelationID($relationID, $relationValue = null, $filteringIDs = null) |
63
|
|
|
{ |
64
|
|
|
// Prepare query |
65
|
|
|
$this->query |
66
|
|
|
->entity($this->relationIdentifier) |
67
|
|
|
->where($this->relationPrimary, $relationID) |
68
|
|
|
->where(self::DELETE_FLAG_FIELD, 1); |
69
|
|
|
|
70
|
|
|
// Add entity identifier filter if passed |
71
|
|
|
if (isset($filteringIDs)) { |
72
|
|
|
$this->query->where($this->primaryField, $filteringIDs); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Perform database query and get only material identifiers collection |
76
|
|
|
return $this->query->fields($this->primaryField); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get current entity instances collection by their identifiers. |
81
|
|
|
* Method can accept different query executors. |
82
|
|
|
* |
83
|
|
|
* @param string|array $entityIDs Entity identifier or their collection |
84
|
|
|
* @param string $executor Method name for query execution |
85
|
|
|
* @return mixed[] Collection of entity instances |
86
|
|
|
*/ |
87
|
|
|
public function byIDs($entityIDs, $executor) |
88
|
|
|
{ |
89
|
|
|
return $this->query |
90
|
|
|
->where($this->primaryField, $entityIDs) |
|
|
|
|
91
|
|
|
->where(self::DELETE_FLAG_FIELD, 1) |
92
|
|
|
->$executor(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieve entities from database. |
97
|
|
|
* |
98
|
|
|
* @param string|array $relationID Relation entity identifier or collection |
99
|
|
|
* @param mixed $relationValue Relation entity value |
100
|
|
|
* @param string $executor Query execution function name |
101
|
|
|
* @return mixed[] Collection of entity instances for this relation identifier |
102
|
|
|
*/ |
103
|
|
|
protected function retrieve($relationID, $relationValue, $executor) |
104
|
|
|
{ |
105
|
|
|
$return = array(); |
106
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
107
|
|
|
if (sizeof($materialIds = $this->idsByRelationID($relationID, $relationValue))) { |
|
|
|
|
108
|
|
|
$return = $this->byIDs($materialIds, $executor); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get current entity instances collection by navigation identifier. |
116
|
|
|
* |
117
|
|
|
* @param string $relationID Relation entity identifier |
118
|
|
|
* @param mixed $relationValue Relation entity value |
119
|
|
|
* @return mixed[] Collection of entity instances |
120
|
|
|
*/ |
121
|
|
|
public function byRelationID($relationID, $relationValue = null) |
122
|
|
|
{ |
123
|
|
|
return $this->retrieve($relationID, $relationValue, 'exec'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get current entity instances amount by navigation identifier. |
128
|
|
|
* |
129
|
|
|
* @param string $relationID Relation entity identifier |
130
|
|
|
* @param mixed $relationValue Relation entity value |
131
|
|
|
* @return integer Amount of entities related to Navigation identifier |
132
|
|
|
*/ |
133
|
|
|
public function amountByRelationID($relationID, $relationValue = null) |
134
|
|
|
{ |
135
|
|
|
return $this->retrieve($relationID, $relationValue, 'count'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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: