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