Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAORelationsTrait.php
Go to the documentation of this file.
1<?php
2
10namespace Ubiquity\orm\traits;
11
18
20
21 abstract protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL);
22
23 public static function _getIncludedForStep($included) {
24 if (\is_bool ( $included )) {
25 return $included;
26 }
27 $ret = [ ];
28 if (\is_array ( $included )) {
29 foreach ( $included as &$includedMember ) {
30 if (\is_array ( $includedMember )) {
31 foreach ( $includedMember as $iMember ) {
32 self::parseEncludeMember ( $ret, $iMember );
33 }
34 } else {
35 self::parseEncludeMember ( $ret, $includedMember );
36 }
37 }
38 }
39 return $ret;
40 }
41
42 private static function parseEncludeMember(&$ret, $includedMember): void {
43 $array = \explode ( '.', $includedMember );
44 $member = \array_shift ( $array );
45 if (\count ( $array ) > 0) {
46 $newValue = \implode ( '.', $array );
47 if ($newValue === '*') {
48 $newValue = true;
49 }
50 if (isset ( $ret [$member] )) {
51 if (! \is_array ( $ret [$member] )) {
52 $ret [$member] = [ $ret [$member] ];
53 }
54 $ret [$member] [] = $newValue;
55 } else {
56 $ret [$member] = $newValue;
57 }
58 } else {
59 if (isset ( $member ) && '' != $member) {
60 $ret [$member] = false;
61 } else {
62 return;
63 }
64 }
65 }
66
67 private static function getInvertedJoinColumns($included, &$invertedJoinColumns): void {
68 foreach ( $invertedJoinColumns as $column => &$annot ) {
69 $member = $annot ['member'];
70 if (isset ( $included [$member] ) === false) {
71 unset ( $invertedJoinColumns [$column] );
72 }
73 }
74 }
75
76 private static function getToManyFields($included, &$toManyFields): void {
77 foreach ( $toManyFields as $member => $annotNotUsed ) {
78 if (isset ( $included [$member] ) === false) {
79 unset ( $toManyFields [$member] );
80 }
81 }
82 }
83
84 public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void {
85 if (isset ( $metaDatas ['#invertedJoinColumn'] )) {
86 $invertedJoinColumns = $metaDatas ['#invertedJoinColumn'];
87 }
88 if (isset ( $metaDatas ['#oneToMany'] )) {
89 $oneToManyFields = $metaDatas ['#oneToMany'];
90 }
91 if (isset ( $metaDatas ['#manyToMany'] )) {
92 $manyToManyFields = $metaDatas ['#manyToMany'];
93 }
94 if (\is_array ( $included )) {
95 if (isset ( $invertedJoinColumns )) {
96 self::getInvertedJoinColumns ( $included, $invertedJoinColumns );
97 }
98 if (isset ( $oneToManyFields )) {
99 self::getToManyFields ( $included, $oneToManyFields );
100 }
101 if (isset ( $manyToManyFields )) {
102 self::getToManyFields ( $included, $manyToManyFields );
103 }
104 }
105 }
106
107 private static function getManyToManyFromArray($instance, $array, $class, $parser): array {
108 $ret = [ ];
109 $continue = true;
110 $accessorToMember = 'get' . \ucfirst ( $parser->getInversedBy () );
111 $myPkAccessor = 'get' . \ucfirst ( $parser->getMyPk () );
112 $pk = self::getFirstKeyValue_ ( $instance );
113
114 if (\count ( $array ) > 0) {
115 $continue = \method_exists ( \current ( $array ), $accessorToMember );
116 }
117 if ($continue) {
118 foreach ( $array as $targetEntityInstance ) {
119 $instances = $targetEntityInstance->$accessorToMember ();
120 if (\is_array ( $instances )) {
121 foreach ( $instances as $inst ) {
122 if ($inst->$myPkAccessor () == $pk)
123 \array_push ( $ret, $targetEntityInstance );
124 }
125 }
126 }
127 } else {
128 Logger::warn ( 'DAO', "L'accesseur au membre " . $parser->getInversedBy () . ' est manquant pour ' . $parser->getTargetEntity (), 'ManyToMany' );
129 }
130 return $ret;
131 }
132
141 public static function getManyToOne($instance, $member, $included = false, $useCache = NULL): ?object {
142 $classname = self::getClass_ ( $instance );
143 if (\is_array ( $instance )) {
144 $instance = self::getById ( $classname, $instance [1], false, $useCache );
145 }
146 $fieldAnnot = OrmUtils::getMemberJoinColumns ( $classname, $member );
147 if ($fieldAnnot !== null) {
148 $annotationArray = $fieldAnnot [1];
149 $member = $annotationArray ['member'];
150 $value = Reflexion::getMemberValue ( $instance, $member );
151 $key = OrmUtils::getFirstKey ( $annotationArray ['className'] );
152 $kv = array ($key => $value );
153 $obj = self::getById ( $annotationArray ['className'], $kv, $included, $useCache );
154 if ($obj !== null) {
155 Logger::info ( 'DAO', 'Loading the member ' . $member . ' for the object ' . $classname, 'getManyToOne' );
156 $accesseur = 'set' . ucfirst ( $member );
157 if (\is_object ( $instance ) && \method_exists ( $instance, $accesseur )) {
158 $instance->$accesseur ( $obj );
159 $instance->_rest [$member] = $obj->_rest;
160 }
161 return $obj;
162 }
163 }
164 return null;
165 }
166
176 public static function getOneToMany($instance, $member, $included = true, $useCache = NULL, $annot = null): array {
177 $ret = array ();
178 $class = self::getClass_ ( $instance );
179 if (! isset ( $annot )) {
180 $annot = OrmUtils::getAnnotationInfoMember ( $class, '#oneToMany', $member );
181 }
182 if ($annot !== false) {
183 $fkAnnot = OrmUtils::getAnnotationInfoMember ( $annot ['className'], '#joinColumn', $annot ['mappedBy'] );
184 if ($fkAnnot !== false) {
185 $fkv = self::getFirstKeyValue_ ( $instance );
186 $db = self::getDb ( $annot ['className'] );
187 $ret = self::_getAll ( $db, $annot ['className'], ConditionParser::simple ( $db->quote . $fkAnnot ['name'] . $db->quote . '= ?', $fkv ), $included, $useCache );
188 if (is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getOneToMany' )) {
189 self::setToMember ( $member, $instance, $ret, $modifier );
190 }
191 }
192 }
193 return $ret;
194 }
195
206 public static function getManyToMany($instance, $member, $included = false, $array = null, $useCache = NULL): array {
207 $ret = [ ];
208 $class = self::getClass_ ( $instance );
209 $db = self::getDb ( $class );
210 $parser = new ManyToManyParser ( $db, $class, $member );
211 if ($parser->init ()) {
212 if (\is_null ( $array )) {
213 $targetEntityClass = $parser->getTargetEntityClass ();
214 $pk = self::getFirstKeyValue_ ( $instance );
215 if ($pk != null) {
216 $quote = $db->quote;
217 $condition = ' INNER JOIN ' . $quote . $parser->getJoinTable () . $quote . ' on ' . $quote . $parser->getJoinTable () . $quote . '.' . $quote . $parser->getFkField () . $quote . '=' . $quote . $parser->getTargetEntityTable () . $quote . '.' . $quote . $parser->getPk () . $quote . ' WHERE ' . $quote . $parser->getJoinTable () . $quote . '.' . $quote . $parser->getMyFkField () . $quote . '= ?';
218 $ret = self::_getAll ( $db, $targetEntityClass, ConditionParser::simple ( $condition, $pk ), $included, $useCache );
219 }
220 } else {
221 $ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser );
222 }
223 if (\is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getManyToMany' )) {
224 self::setToMember ( $member, $instance, $ret, $modifier );
225 }
226 }
227 return $ret;
228 }
229
236 public static function affectsManyToManys($instance, $array = NULL, $useCache = NULL) {
237 $metaDatas = OrmUtils::getModelMetadata ( \get_class ( $instance ) );
238 $manyToManyFields = $metaDatas ['#manyToMany'];
239 if (\count ( $manyToManyFields ) > 0) {
240 foreach ( $manyToManyFields as $member ) {
241 self::getManyToMany ( $instance, $member, false, $array, $useCache );
242 }
243 }
244 }
245}
Ubiquity Generic database class.
Definition Database.php:25
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
static getToManyFields($included, &$toManyFields)
static getManyToManyFromArray($instance, $array, $class, $parser)
static affectsManyToManys($instance, $array=NULL, $useCache=NULL)
static getManyToOne($instance, $member, $included=false, $useCache=NULL)
Loads member associated with $instance by a ManyToOne relationship.
static getInvertedJoinColumns($included, &$invertedJoinColumns)
static _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields)
static getOneToMany($instance, $member, $included=true, $useCache=NULL, $annot=null)
Assign / load the child records in the $member member of $instance.
static _getAll(Database $db, $className, ConditionParser $conditionParser, $included=true, $useCache=NULL)
static parseEncludeMember(&$ret, $includedMember)
static getManyToMany($instance, $member, $included=false, $array=null, $useCache=NULL)
Assigns / loads the child records in the $member member of $instance.
Ubiquity\orm\traits This class is part of Ubiquity.