|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\log\Logger; |
|
7
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
8
|
|
|
|
|
9
|
|
|
trait DAORelationsAssignmentsTrait { |
|
10
|
6 |
|
protected static function setToMember($member, $instance, $value, $accessor) { |
|
11
|
6 |
|
$instance->$accessor($value); |
|
12
|
6 |
|
$instance->_rest[$member]=$value; |
|
13
|
6 |
|
} |
|
14
|
|
|
|
|
15
|
6 |
|
protected static function getAccessor($member, $instance,$part) { |
|
16
|
6 |
|
$accessor="set" . ucfirst($member); |
|
17
|
6 |
|
if (method_exists($instance, $accessor)) { |
|
18
|
6 |
|
return $accessor; |
|
19
|
|
|
} |
|
20
|
|
|
$class=get_class($instance); |
|
21
|
|
|
Logger::warn("DAO", "Missing modifier " . $accessor . " in " . $class,$part); |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
5 |
|
protected static function _affectsRelationObjects($className,$classPropKey,$manyToOneQueries,$oneToManyQueries,$manyToManyParsers,$objects,$included,$useCache){ |
|
26
|
5 |
|
if(\sizeof($manyToOneQueries)>0){ |
|
27
|
|
|
self::_affectsObjectsFromArray($manyToOneQueries,$included, function($object,$member,$manyToOneObjects,$fkField,$accessor){ |
|
28
|
5 |
|
self::affectsManyToOneFromArray($object,$member,$manyToOneObjects,$fkField,$accessor); |
|
29
|
5 |
|
},'getManyToOne'); |
|
30
|
|
|
} |
|
31
|
5 |
|
if(\sizeof($oneToManyQueries)>0){ |
|
32
|
|
|
self::_affectsObjectsFromArray($oneToManyQueries,$included, function($object,$member,$relationObjects,$fkField,$accessor,$class,$prop) use ($classPropKey){ |
|
33
|
4 |
|
self::affectsOneToManyFromArray($object,$member,$relationObjects,$fkField,$accessor,$class,$prop,$classPropKey); |
|
34
|
4 |
|
},'getOneToMany'); |
|
35
|
|
|
} |
|
36
|
5 |
|
if(\sizeof($manyToManyParsers)>0){ |
|
37
|
4 |
|
self::_affectsManyToManyObjectsFromArray($className,$manyToManyParsers, $objects,$included,$useCache); |
|
38
|
|
|
} |
|
39
|
5 |
|
} |
|
40
|
|
|
|
|
41
|
5 |
|
private static function affectsManyToOneFromArray($object,$member,$manyToOneObjects,$fkField,$accessor){ |
|
42
|
5 |
|
if(isset($object->$fkField)){ |
|
43
|
5 |
|
$value=$manyToOneObjects[$object->$fkField]; |
|
44
|
5 |
|
self::setToMember($member, $object, $value, $accessor); |
|
45
|
|
|
} |
|
46
|
5 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param object $instance |
|
50
|
|
|
* @param string $member |
|
51
|
|
|
* @param array $array |
|
52
|
|
|
* @param string $mappedByAccessor |
|
53
|
|
|
* @param string $class |
|
54
|
|
|
* @param \ReflectionProperty $prop |
|
55
|
|
|
*/ |
|
56
|
4 |
|
private static function affectsOneToManyFromArray($instance, $member, $array=null, $mappedByAccessor=null,$accessor="",$class="",$prop=null,$classPropKey=null) { |
|
57
|
4 |
|
$ret=[]; |
|
58
|
4 |
|
self::_getOneToManyFromArray($ret,$array, Reflexion::getPropValue($instance,$classPropKey), $mappedByAccessor,$prop); |
|
59
|
4 |
|
self::setToMember($member, $instance, $ret, $accessor); |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
5 |
|
private static function _affectsObjectsFromArray($queries,$included,$affectsCallback,$part,$useCache=NULL){ |
|
63
|
5 |
|
$includedNext=false; |
|
64
|
5 |
|
foreach ($queries as $key=>$pendingRelationsRequest){ |
|
65
|
5 |
|
list($class,$member,$fkField)=\explode("|", $key); |
|
66
|
5 |
|
if(is_array($included)){ |
|
67
|
|
|
$includedNext=self::_getIncludedNext($included, $member); |
|
68
|
|
|
} |
|
69
|
5 |
|
$objectsParsers=$pendingRelationsRequest->getObjectsConditionParsers(); |
|
70
|
5 |
|
$prop=null; |
|
71
|
5 |
|
if('getOneToMany'===$part){ |
|
72
|
4 |
|
$prop=OrmUtils::getFirstPropKey($class); |
|
73
|
4 |
|
$fkField="get" . ucfirst($fkField); |
|
74
|
|
|
} |
|
75
|
5 |
|
foreach ($objectsParsers as $objectsConditionParser){ |
|
76
|
5 |
|
$objectsConditionParser->compileParts(); |
|
77
|
5 |
|
$relationObjects=self::_getAll($class,$objectsConditionParser->getConditionParser(),$includedNext,$useCache); |
|
78
|
5 |
|
$objects=$objectsConditionParser->getObjects(); |
|
79
|
5 |
|
if($accessor=self::getAccessor($member, current($objects), $part)){ |
|
80
|
5 |
|
foreach ($objects as $object){ |
|
81
|
5 |
|
$affectsCallback($object, $member,$relationObjects,$fkField,$accessor,$class,$prop); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
5 |
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
private static function _affectsManyToManyObjectsFromArray($objectsClass,$parsers,$objects,$included,$useCache=NULL){ |
|
89
|
4 |
|
$includedNext=false; |
|
90
|
4 |
|
$prop=OrmUtils::getFirstPropKey($objectsClass); |
|
91
|
4 |
|
foreach ($parsers as $key=>$parser){ |
|
92
|
4 |
|
list($class,$member)=\explode("|", $key); |
|
93
|
4 |
|
if(is_array($included)){ |
|
94
|
|
|
$includedNext=self::_getIncludedNext($included, $member); |
|
95
|
|
|
} |
|
96
|
4 |
|
$myPkValues=[]; |
|
97
|
4 |
|
$cParser=self::generateManyToManyParser($parser, $myPkValues); |
|
98
|
4 |
|
$relationObjects=self::_getAll($class,$cParser,$includedNext,$useCache); |
|
99
|
4 |
|
if($accessor=self::getAccessor($member, current($objects), 'getManyToMany')){ |
|
100
|
4 |
|
foreach ($objects as $object){ |
|
101
|
4 |
|
$pkV=Reflexion::getPropValue($object, $prop); |
|
102
|
4 |
|
if(isset($myPkValues[$pkV])){ |
|
103
|
4 |
|
$ret=self::getManyToManyFromArrayIds($class,$relationObjects, $myPkValues[$pkV]); |
|
104
|
4 |
|
self::setToMember($member, $object, $ret, $accessor); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
4 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|