Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAORelationsAssignmentsTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
10
21
22 protected static function setToMember($member, $instance, $value, $accessor) {
23 $instance->$accessor ( $value );
24 $instance->_rest [$member] = $value;
25 }
26
27 protected static function getAccessor($member, $instance, $part) {
28 $accessor = "set" . ucfirst ( $member );
29 if (method_exists ( $instance, $accessor )) {
30 return $accessor;
31 }
32 $class = get_class ( $instance );
33 Logger::warn ( "DAO", "Missing modifier " . $accessor . " in " . $class, $part );
34 return false;
35 }
36
37 public static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache): void {
38 if (\count ( $manyToOneQueries ) > 0) {
39 self::_affectsObjectsFromArray ( $manyToOneQueries, $included, function ($object, $member, $manyToOneObjects, $fkField, $accessor) {
40 self::affectsManyToOneFromArray ( $object, $member, $manyToOneObjects, $fkField, $accessor );
41 }, 'getManyToOne' );
42 }
43 if (\count ( $oneToManyQueries ) > 0) {
44 self::_affectsObjectsFromArray ( $oneToManyQueries, $included, function ($object, $member, $relationObjects, $fkField, $accessor, $class, $prop) use ($classPropKey) {
45 self::affectsOneToManyFromArray ( $object, $member, $relationObjects, $fkField, $accessor, $class, $prop, $classPropKey );
46 }, 'getOneToMany' );
47 }
48 if (\count ( $manyToManyParsers ) > 0) {
49 self::_affectsManyToManyObjectsFromArray ( $className, $manyToManyParsers, $objects, $included, $useCache );
50 }
51 }
52
53 private static function affectsManyToOneFromArray($object, $member, $manyToOneObjects, $fkField, $accessor) {
54 if (isset ( $object->$fkField )) {
55 $value = $manyToOneObjects [$object->$fkField];
56 self::setToMember ( $member, $object, $value, $accessor );
57 }
58 }
59
69 private static function affectsOneToManyFromArray($instance, $member, $array = null, $mappedByAccessor = null, $accessor = '', $class = '', $prop = null, $classPropKey = null) {
70 $ret = [ ];
71 self::_getOneToManyFromArray ( $ret, $array, Reflexion::getPropValue ( $instance, $classPropKey ), $mappedByAccessor, $prop );
72 self::setToMember ( $member, $instance, $ret, $accessor );
73 }
74
75 private static function _affectsObjectsFromArray($queries, $included, $affectsCallback, $part, $useCache = NULL) {
76 $includedNext = false;
77 foreach ( $queries as $key => $pendingRelationsRequest ) {
78 list ( $class, $member, $fkField, $fkClass ) = \explode ( '|', $key );
79 if (\is_array ( $included )) {
80 $includedNext = self::_getIncludedNext ( $included, $member );
81 }
82 $objectsParsers = $pendingRelationsRequest->getObjectsConditionParsers ();
83 $prop = null;
84 if ('getOneToMany' === $part) {
85 $prop = OrmUtils::getFirstPropKey ( $fkClass );
86 $fkField = 'get' . \ucfirst ( $fkField );
87 }
88 foreach ( $objectsParsers as $objectsConditionParser ) {
89 $objectsConditionParser->compileParts ();
90 $relationObjects = self::_getAll ( self::getDb ( $class ), $class, $objectsConditionParser->getConditionParser (), $includedNext, $useCache );
91 $objects = $objectsConditionParser->getObjects ();
92 if ($accessor = self::getAccessor ( $member, current ( $objects ), $part )) {
93 foreach ( $objects as $object ) {
94 $affectsCallback ( $object, $member, $relationObjects, $fkField, $accessor, $class, $prop );
95 }
96 }
97 }
98 }
99 }
100
101 private static function _affectsManyToManyObjectsFromArray($objectsClass, $parsers, $objects, $included, $useCache = NULL) {
102 $includedNext = false;
103 $prop = OrmUtils::getFirstPropKey ( $objectsClass );
104 foreach ( $parsers as $key => $parser ) {
105 list ( $class, $member ) = \explode ( "|", $key );
106 if (is_array ( $included )) {
107 $includedNext = self::_getIncludedNext ( $included, $member );
108 }
109 $myPkValues = [ ];
110 $cParser = self::generateManyToManyParser ( $parser, $myPkValues );
111 $relationObjects = self::_getAll ( self::getDb ( $class ), $class, $cParser, $includedNext, $useCache );
112 if ($accessor = self::getAccessor ( $member, current ( $objects ), 'getManyToMany' )) {
113 foreach ( $objects as $object ) {
114 $pkV = Reflexion::getPropValue ( $object, $prop );
115 if (isset ( $myPkValues [$pkV] )) {
116 $ret = self::getManyToManyFromArrayIds ( $class, $relationObjects, $myPkValues [$pkV] );
117 self::setToMember ( $member, $object, $ret, $accessor );
118 }
119 }
120 }
121 }
122 }
123
124 private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
125 foreach ( $array as $element ) {
126 $elementRef = $element->$elementAccessor ();
127 if (($elementRef == $fkv) || (\is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
128 $ret [] = $element;
129 }
130 }
131 }
132
133 private static function generateManyToManyParser(ManyToManyParser $parser, &$myPkValues): ConditionParser {
134 $sql = $parser->generateConcatSQL ();
135 $result = self::getDb ( $parser->getTargetEntityClass () )->prepareAndFetchAll ( $sql, $parser->getWhereValues () );
136 $condition = $parser->getParserWhereMask ( ' ?' );
137 $cParser = new ConditionParser ();
138 foreach ( $result as $row ) {
139 $values = explode ( ',', $row ['_concat'] );
140 $myPkValues [$row ['_field']] = $values;
141 $cParser->addParts ( $condition, $values );
142 }
143 $cParser->compileParts ();
144 return $cParser;
145 }
146
147 private static function _getIncludedNext($included, $member) {
148 return (isset ( $included [$member] )) ? (\is_bool ( $included [$member] ) ? $included [$member] : [ $included [$member] ]) : false;
149 }
150
151 private static function getManyToManyFromArrayIds($objectClass, $relationObjects, $ids): array {
152 $ret = [ ];
153 $prop = OrmUtils::getFirstPropKey ( $objectClass );
154 foreach ( $relationObjects as $targetEntityInstance ) {
155 $id = Reflexion::getPropValue ( $targetEntityInstance, $prop );
156 if (\array_search ( $id, $ids ) !== false) {
157 \array_push ( $ret, $targetEntityInstance );
158 }
159 }
160 return $ret;
161 }
162}
163
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Object/relational mapping utilities.
Definition OrmUtils.php:17
Represents a query condition.
Reflection utilities in dev environment only.
Definition Reflexion.php:17
Used by DAO class, realize relations assignments.
static _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop)
static generateManyToManyParser(ManyToManyParser $parser, &$myPkValues)
static affectsOneToManyFromArray($instance, $member, $array=null, $mappedByAccessor=null, $accessor='', $class='', $prop=null, $classPropKey=null)
static _affectsObjectsFromArray($queries, $included, $affectsCallback, $part, $useCache=NULL)
static affectsManyToOneFromArray($object, $member, $manyToOneObjects, $fkField, $accessor)
static _affectsManyToManyObjectsFromArray($objectsClass, $parsers, $objects, $included, $useCache=NULL)
static getManyToManyFromArrayIds($objectClass, $relationObjects, $ids)
static _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache)
static setToMember($member, $instance, $value, $accessor)
Ubiquity\orm\traits This class is part of Ubiquity.