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
|
6 |
|
private static function generateManyToManyParser(ManyToManyParser $parser,&$myPkValues){ |
18
|
6 |
|
$sql=$parser->generateConcatSQL(); |
19
|
6 |
|
$result=self::$db->prepareAndFetchAll($sql,$parser->getWhereValues()); |
20
|
6 |
|
$condition=$parser->getParserWhereMask(" ?"); |
21
|
6 |
|
$cParser=new ConditionParser(); |
22
|
6 |
|
foreach ($result as $row){ |
23
|
6 |
|
$values=explode(",", $row["_concat"]); |
24
|
6 |
|
$myPkValues[$row["_field"]]=$values; |
25
|
6 |
|
$cParser->addParts($condition, $values); |
26
|
|
|
} |
27
|
6 |
|
$cParser->compileParts(); |
28
|
6 |
|
return $cParser; |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
private static function _getIncludedNext($included,$member){ |
32
|
2 |
|
return (isset($included[$member]))?(is_bool($included[$member])?$included[$member]:[$included[$member]]):false; |
33
|
|
|
} |
34
|
|
|
|
35
|
6 |
|
private static function getManyToManyFromArrayIds($objectClass,$relationObjects, $ids){ |
36
|
6 |
|
$ret=[]; |
37
|
6 |
|
$prop=OrmUtils::getFirstPropKey($objectClass); |
38
|
6 |
|
foreach ( $relationObjects as $targetEntityInstance ) { |
39
|
6 |
|
$id=Reflexion::getPropValue($targetEntityInstance,$prop); |
40
|
6 |
|
if (array_search($id, $ids)!==false) { |
41
|
6 |
|
array_push($ret, $targetEntityInstance); |
42
|
|
|
} |
43
|
|
|
} |
44
|
6 |
|
return $ret; |
45
|
|
|
} |
46
|
|
|
|
47
|
20 |
|
protected static function getIncludedForStep($included){ |
48
|
20 |
|
if(is_bool($included)){ |
49
|
20 |
|
return $included; |
50
|
|
|
} |
51
|
4 |
|
$ret=[]; |
52
|
4 |
|
if(is_array($included)){ |
53
|
4 |
|
foreach ($included as &$includedMember){ |
54
|
4 |
|
if(is_array($includedMember)){ |
55
|
|
|
foreach ($includedMember as $iMember){ |
56
|
|
|
self::parseEncludeMember($ret, $iMember); |
57
|
|
|
} |
58
|
|
|
}else{ |
59
|
4 |
|
self::parseEncludeMember($ret, $includedMember); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
4 |
|
return $ret; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
private static function parseEncludeMember(&$ret,$includedMember){ |
67
|
4 |
|
$array=explode(".", $includedMember); |
68
|
4 |
|
$member=array_shift($array); |
69
|
4 |
|
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
|
4 |
|
if(isset($member) && ""!=$member){ |
84
|
4 |
|
$ret[$member]=false; |
85
|
|
|
}else{ |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
} |
89
|
4 |
|
} |
90
|
|
|
|
91
|
1 |
|
private static function getInvertedJoinColumns($included,&$invertedJoinColumns){ |
92
|
1 |
|
foreach ($invertedJoinColumns as $column=>&$annot){ |
93
|
1 |
|
$member=$annot["member"]; |
94
|
1 |
|
if(isset($included[$member])===false){ |
95
|
1 |
|
unset($invertedJoinColumns[$column]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
1 |
|
} |
99
|
|
|
|
100
|
4 |
|
private static function getToManyFields($included,&$toManyFields){ |
101
|
4 |
|
foreach ($toManyFields as $member=>$annotNotUsed){ |
102
|
4 |
|
if(isset($included[$member])===false){ |
103
|
4 |
|
unset($toManyFields[$member]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
4 |
|
} |
107
|
|
|
|
108
|
12 |
|
protected static function _initRelationFields($included,$metaDatas,&$invertedJoinColumns,&$oneToManyFields,&$manyToManyFields){ |
109
|
12 |
|
if (isset($metaDatas["#invertedJoinColumn"])){ |
110
|
7 |
|
$invertedJoinColumns=$metaDatas["#invertedJoinColumn"]; |
111
|
|
|
} |
112
|
12 |
|
if (isset($metaDatas["#oneToMany"])) { |
113
|
11 |
|
$oneToManyFields=$metaDatas["#oneToMany"]; |
114
|
|
|
} |
115
|
12 |
|
if (isset($metaDatas["#manyToMany"])) { |
116
|
6 |
|
$manyToManyFields=$metaDatas["#manyToMany"]; |
117
|
|
|
} |
118
|
12 |
|
if(is_array($included)){ |
119
|
4 |
|
if(isset($invertedJoinColumns)){ |
120
|
1 |
|
self::getInvertedJoinColumns($included, $invertedJoinColumns); |
121
|
|
|
} |
122
|
4 |
|
if(isset($oneToManyFields)){ |
123
|
4 |
|
self::getToManyFields($included, $oneToManyFields); |
124
|
|
|
} |
125
|
4 |
|
if(isset($manyToManyFields)){ |
126
|
1 |
|
self::getToManyFields($included, $manyToManyFields); |
127
|
|
|
} |
128
|
|
|
} |
129
|
12 |
|
} |
130
|
|
|
} |
131
|
|
|
|