|
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 Relational extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string Related entity primary field */ |
|
21
|
|
|
protected $relationPrimary; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string Relation entity identifier */ |
|
24
|
|
|
protected $relationIdentifier; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Entity constructor. |
|
28
|
|
|
* @param QueryInterface $query Database query instance |
|
29
|
|
|
* @param string $identifier Entity identifier |
|
30
|
|
|
* @param string $relationPrimary Relation entity primary field name |
|
31
|
|
|
* @param string $relationIdentifier Relation entity identifier |
|
32
|
|
|
* @param array $filteringIDs Collection of entity identifiers for filtering |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
QueryInterface $query, |
|
36
|
|
|
$identifier, |
|
37
|
|
|
$relationPrimary, |
|
38
|
|
|
$relationIdentifier, |
|
39
|
|
|
$filteringIDs = array() |
|
40
|
|
|
) { |
|
41
|
|
|
parent::__construct($query, $identifier, $filteringIDs); |
|
42
|
|
|
|
|
43
|
|
|
$this->relationPrimary = $relationPrimary; |
|
44
|
|
|
$this->relationIdentifier = $relationIdentifier; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get current entity identifiers collection by navigation identifier. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $relationID Relation entity identifier |
|
51
|
|
|
* @param mixed $relationValue Relation entity value |
|
52
|
|
|
* @param array $filteringIDs Collection of entity identifiers for filtering query |
|
53
|
|
|
* @return array Collection of entity identifiers filtered by navigation identifier. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function idsByRelationID($relationID, $relationValue = null, array $filteringIDs = array()) |
|
56
|
|
|
{ |
|
57
|
|
|
// Prepare query |
|
58
|
|
|
$this->query |
|
59
|
|
|
->entity($this->relationIdentifier) |
|
60
|
|
|
->where($this->relationPrimary, $relationID) |
|
61
|
|
|
->where(self::DELETE_FLAG_FIELD, 1); |
|
62
|
|
|
|
|
63
|
|
|
// Add entity identifier filter if passed |
|
64
|
|
|
if (sizeof($filteringIDs)) { |
|
65
|
|
|
$this->query->where($this->primaryField, $filteringIDs); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Perform database query and get only material identifiers collection |
|
69
|
|
|
return $this->query->fields($this->primaryField); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieve entities from database. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string|array $relationID Relation entity identifier or collection |
|
76
|
|
|
* @param mixed $relationValue Relation entity value |
|
77
|
|
|
* @param string $executor Query execution function name |
|
78
|
|
|
* @return mixed[] Collection of entity instances for this relation identifier |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function retrieve($relationID, $relationValue, $executor) |
|
81
|
|
|
{ |
|
82
|
|
|
$return = array(); |
|
83
|
|
|
/** @var array $ids Collection of entity identifiers filtered by additional field */ |
|
84
|
|
|
if (sizeof($ids = $this->idsByRelationID($relationID, $relationValue, $this->filteringIDs))) { |
|
|
|
|
|
|
85
|
|
|
$return = $this->byIDs($ids, $executor); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get current entity instances collection by navigation identifier. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $relationID Relation entity identifier |
|
95
|
|
|
* @param mixed $relationValue Relation entity value |
|
96
|
|
|
* @return mixed[] Collection of entity instances |
|
97
|
|
|
*/ |
|
98
|
|
|
public function byRelationID($relationID, $relationValue = null) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->retrieve($relationID, $relationValue, 'exec'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get current entity instances amount by navigation identifier. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $relationID Relation entity identifier |
|
107
|
|
|
* @param mixed $relationValue Relation entity value |
|
108
|
|
|
* @return integer Amount of entities related to Navigation identifier |
|
109
|
|
|
*/ |
|
110
|
|
|
public function amountByRelationID($relationID, $relationValue = null) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->retrieve($relationID, $relationValue, 'count'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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: