24 if (\is_bool ( $included )) {
28 if (\is_array ( $included )) {
29 foreach ( $included as &$includedMember ) {
30 if (\is_array ( $includedMember )) {
31 foreach ( $includedMember as $iMember ) {
43 $array = \explode (
'.', $includedMember );
44 $member = \array_shift ( $array );
45 if (\count ( $array ) > 0) {
46 $newValue = \implode (
'.', $array );
47 if ($newValue ===
'*') {
50 if (isset ( $ret [$member] )) {
51 if (! \is_array ( $ret [$member] )) {
52 $ret [$member] = [ $ret [$member] ];
54 $ret [$member] [] = $newValue;
56 $ret [$member] = $newValue;
59 if (isset ( $member ) &&
'' != $member) {
60 $ret [$member] =
false;
68 foreach ( $invertedJoinColumns as $column => &$annot ) {
69 $member = $annot [
'member'];
70 if (isset ( $included [$member] ) ===
false) {
71 unset ( $invertedJoinColumns [$column] );
77 foreach ( $toManyFields as $member => $annotNotUsed ) {
78 if (isset ( $included [$member] ) ===
false) {
79 unset ( $toManyFields [$member] );
84 public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void {
85 if (isset ( $metaDatas [
'#invertedJoinColumn'] )) {
86 $invertedJoinColumns = $metaDatas [
'#invertedJoinColumn'];
88 if (isset ( $metaDatas [
'#oneToMany'] )) {
89 $oneToManyFields = $metaDatas [
'#oneToMany'];
91 if (isset ( $metaDatas [
'#manyToMany'] )) {
92 $manyToManyFields = $metaDatas [
'#manyToMany'];
94 if (\is_array ( $included )) {
95 if (isset ( $invertedJoinColumns )) {
96 self::getInvertedJoinColumns ( $included, $invertedJoinColumns );
98 if (isset ( $oneToManyFields )) {
99 self::getToManyFields ( $included, $oneToManyFields );
101 if (isset ( $manyToManyFields )) {
102 self::getToManyFields ( $included, $manyToManyFields );
110 $accessorToMember =
'get' . \ucfirst ( $parser->getInversedBy () );
111 $myPkAccessor =
'get' . \ucfirst ( $parser->getMyPk () );
112 $pk = self::getFirstKeyValue_ ( $instance );
114 if (\count ( $array ) > 0) {
115 $continue = \method_exists ( \current ( $array ), $accessorToMember );
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 );
128 Logger::warn (
'DAO',
"L'accesseur au membre " . $parser->getInversedBy () .
' est manquant pour ' . $parser->getTargetEntity (),
'ManyToMany' );
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 );
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 );
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;
176 public static function getOneToMany($instance, $member, $included =
true, $useCache = NULL, $annot =
null): array {
178 $class = self::getClass_ ( $instance );
179 if (! isset ( $annot )) {
180 $annot = OrmUtils::getAnnotationInfoMember ( $class,
'#oneToMany', $member );
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 );
206 public static function getManyToMany($instance, $member, $included =
false, $array =
null, $useCache = NULL): array {
208 $class = self::getClass_ ( $instance );
209 $db = self::getDb ( $class );
211 if ($parser->init ()) {
212 if (\is_null ( $array )) {
213 $targetEntityClass = $parser->getTargetEntityClass ();
214 $pk = self::getFirstKeyValue_ ( $instance );
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 );
221 $ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser );
223 if (\is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance,
'getManyToMany' )) {
224 self::setToMember ( $member, $instance, $ret, $modifier );
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 );
Ubiquity Generic database class.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Object/relational mapping utilities.
Represents a query condition.
Reflection utilities in dev environment only.
static _getIncludedForStep($included)
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.