|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
|
7
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
|
8
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author jc |
|
12
|
|
|
* @property \Ubiquity\db\Database $db |
|
13
|
|
|
*/ |
|
14
|
|
|
trait DAORelationsTrait { |
|
15
|
|
|
abstract protected static function _getAll($className, ConditionParser $conditionParser, $included=true,$useCache=NULL); |
|
16
|
|
|
|
|
17
|
5 |
|
private static function generateManyToManyParser(ManyToManyParser $parser,&$myPkValues){ |
|
18
|
5 |
|
$sql=$parser->generateConcatSQL(); |
|
19
|
5 |
|
$result=self::$db->prepareAndFetchAll($sql,$parser->getWhereValues()); |
|
20
|
5 |
|
$condition=$parser->getParserWhereMask(" ?"); |
|
21
|
5 |
|
$cParser=new ConditionParser(); |
|
22
|
5 |
|
foreach ($result as $row){ |
|
23
|
5 |
|
$values=explode(",", $row["_concat"]); |
|
24
|
5 |
|
$myPkValues[$row["_field"]]=$values; |
|
25
|
5 |
|
$cParser->addParts($condition, $values); |
|
26
|
|
|
} |
|
27
|
5 |
|
$cParser->compileParts(); |
|
28
|
5 |
|
return $cParser; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private static function _getIncludedNext($included,$member){ |
|
32
|
|
|
return (isset($included[$member]))?(is_bool($included[$member])?$included[$member]:[$included[$member]]):false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
private static function getManyToManyFromArrayIds($objectClass,$relationObjects, $ids){ |
|
36
|
5 |
|
$ret=[]; |
|
37
|
5 |
|
$prop=OrmUtils::getFirstPropKey($objectClass); |
|
38
|
5 |
|
foreach ( $relationObjects as $targetEntityInstance ) { |
|
39
|
5 |
|
$id=Reflexion::getPropValue($targetEntityInstance,$prop); |
|
40
|
5 |
|
if (array_search($id, $ids)!==false) { |
|
41
|
5 |
|
array_push($ret, $targetEntityInstance); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
5 |
|
return $ret; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
10 |
|
protected static function getIncludedForStep($included){ |
|
48
|
10 |
|
if(is_bool($included)){ |
|
49
|
10 |
|
return $included; |
|
50
|
|
|
} |
|
51
|
1 |
|
$ret=[]; |
|
52
|
1 |
|
if(is_array($included)){ |
|
53
|
1 |
|
foreach ($included as &$includedMember){ |
|
54
|
1 |
|
if(is_array($includedMember)){ |
|
55
|
|
|
foreach ($includedMember as $iMember){ |
|
56
|
|
|
self::parseEncludeMember($ret, $iMember); |
|
57
|
|
|
} |
|
58
|
|
|
}else{ |
|
59
|
1 |
|
self::parseEncludeMember($ret, $includedMember); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
1 |
|
return $ret; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
private static function parseEncludeMember(&$ret,$includedMember){ |
|
67
|
1 |
|
$array=explode(".", $includedMember); |
|
68
|
1 |
|
$member=array_shift($array); |
|
69
|
1 |
|
if(sizeof($array)>0){ |
|
70
|
|
|
$newValue=implode(".", $array); |
|
71
|
|
|
if($newValue==='*'){ |
|
72
|
|
|
$newValue=true; |
|
73
|
|
|
} |
|
74
|
|
|
if(isset($ret[$member])){ |
|
75
|
|
|
if(!is_array($ret[$member])){ |
|
76
|
|
|
$ret[$member]=[$ret[$member]]; |
|
77
|
|
|
} |
|
78
|
|
|
$ret[$member][]=$newValue; |
|
79
|
|
|
}else{ |
|
80
|
|
|
$ret[$member]=$newValue; |
|
81
|
|
|
} |
|
82
|
|
|
}else{ |
|
83
|
1 |
|
if(isset($member) && ""!=$member){ |
|
84
|
1 |
|
$ret[$member]=false; |
|
85
|
|
|
}else{ |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
private static function getInvertedJoinColumns($included,&$invertedJoinColumns){ |
|
92
|
|
|
foreach ($invertedJoinColumns as $column=>&$annot){ |
|
93
|
|
|
$member=$annot["member"]; |
|
94
|
|
|
if(isset($included[$member])===false){ |
|
95
|
|
|
unset($invertedJoinColumns[$column]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
private static function getToManyFields($included,&$toManyFields){ |
|
101
|
1 |
|
foreach ($toManyFields as $member=>$annotNotUsed){ |
|
102
|
1 |
|
if(isset($included[$member])===false){ |
|
103
|
1 |
|
unset($toManyFields[$member]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
7 |
|
protected static function _initRelationFields($included,$metaDatas,&$invertedJoinColumns,&$oneToManyFields,&$manyToManyFields){ |
|
109
|
7 |
|
if (isset($metaDatas["#invertedJoinColumn"])){ |
|
110
|
6 |
|
$invertedJoinColumns=$metaDatas["#invertedJoinColumn"]; |
|
111
|
|
|
} |
|
112
|
7 |
|
if (isset($metaDatas["#oneToMany"])) { |
|
113
|
6 |
|
$oneToManyFields=$metaDatas["#oneToMany"]; |
|
114
|
|
|
} |
|
115
|
7 |
|
if (isset($metaDatas["#manyToMany"])) { |
|
116
|
5 |
|
$manyToManyFields=$metaDatas["#manyToMany"]; |
|
117
|
|
|
} |
|
118
|
7 |
|
if(is_array($included)){ |
|
119
|
1 |
|
if(isset($invertedJoinColumns)){ |
|
120
|
|
|
self::getInvertedJoinColumns($included, $invertedJoinColumns); |
|
121
|
|
|
} |
|
122
|
1 |
|
if(isset($oneToManyFields)){ |
|
123
|
1 |
|
self::getToManyFields($included, $oneToManyFields); |
|
124
|
|
|
} |
|
125
|
1 |
|
if(isset($manyToManyFields)){ |
|
126
|
|
|
self::getToManyFields($included, $manyToManyFields); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
7 |
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|