Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelJoinBuilder.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
45  protected $modelAttributeToDataProviderAdapter;
46 
50  protected $joinTablesAdapter;
51 
55  protected $setDistinct;
56 
60  protected $resolvedOnTableAliasName;
61 
68  protected $tableAliasNameForBaseModel;
69 
76  protected $tableAliasNameForRelatedModel;
77 
83  public static function makeColumnNameWithTableAlias($tableAliasName, $columnName)
84  {
85  assert('is_string($tableAliasName)');
86  assert('is_string($columnName)');
88  return $quote . $tableAliasName . $quote . '.' . $quote . $columnName . $quote;
89  }
90 
95  protected static function resolveForeignKey($idName)
96  {
97  assert('is_string($idName)');
98  return $idName . '_id';
99  }
100 
107  $modelAttributeToDataProviderAdapter,
109  $joinTablesAdapter,
110  $setDistinct = false)
111  {
112  $this->modelAttributeToDataProviderAdapter = $modelAttributeToDataProviderAdapter;
113  $this->joinTablesAdapter = $joinTablesAdapter;
114  $this->setDistinct = $setDistinct;
115  }
116 
121  {
122  return $this->tableAliasNameForBaseModel;
123  }
124 
130  {
131  if (!$this->modelAttributeToDataProviderAdapter->hasRelatedAttribute())
132  {
133  throw new NotSupportedException();
134  }
135  return $this->tableAliasNameForRelatedModel;
136  }
137 
143  public function resolveJoins($onTableAliasName = null, $canUseFromJoins = true)
144  {
145  assert('is_string($onTableAliasName) || $onTableAliasName == null');
146  assert('is_bool($canUseFromJoins)');
147  $onTableAliasName = $this->resolveOnTableAliasName($onTableAliasName);
148  $this->tableAliasNameForBaseModel = $onTableAliasName;
149  $onTableAliasName = $this->resolveJoinsForAttribute($onTableAliasName, $canUseFromJoins);
150  $this->resolvedOnTableAliasName = $onTableAliasName;
151  if ($this->modelAttributeToDataProviderAdapter->hasRelatedAttribute())
152  {
153  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
154  $this->modelAttributeToDataProviderAdapter->
155  getRelationModelClassNameThatCanHaveATable(),
156  $this->modelAttributeToDataProviderAdapter->
157  getRelatedAttribute());
158  $builderClassName = get_class($this);
159  $builder = new $builderClassName($modelAttributeToDataProviderAdapter,
160  $this->joinTablesAdapter);
161  $this->tableAliasNameForRelatedModel = $onTableAliasName;
162  $onTableAliasName = $builder->resolveJoinsForAttribute($onTableAliasName, false);
163  }
164  return $onTableAliasName;
165  }
166 
172  public function resolveOnlyAttributeJoins($onTableAliasName = null, $canUseFromJoins = true)
173  {
174  assert('is_string($onTableAliasName) || $onTableAliasName == null');
175  assert('is_bool($canUseFromJoins)');
176  $onTableAliasName = $this->resolveOnTableAliasName($onTableAliasName);
177  $onTableAliasName = $this->resolveJoinsForAttribute($onTableAliasName, $canUseFromJoins);
178  $this->resolvedOnTableAliasName = $onTableAliasName;
179  return $onTableAliasName;
180  }
181 
186  public function resolveOnTableAliasName($onTableAliasName = null)
187  {
188  if ($onTableAliasName == null)
189  {
190  $onTableAliasName = $this->resolveOnTableAliasNameForDerivedRelationViaCastedUpModel();
191  }
192  return $onTableAliasName;
193  }
194 
200  protected function resolveJoinsForAttribute($onTableAliasName, $canUseFromJoins = true)
201  {
202  assert('is_string($onTableAliasName)');
203  assert('is_bool($canUseFromJoins)');
204  if ($this->modelAttributeToDataProviderAdapter->isAttributeDerivedRelationViaCastedUpModel())
205  {
206  return $this->resolveJoinsForDerivedRelationViaCastedUpModel($onTableAliasName, $canUseFromJoins);
207  }
208  elseif ($this->modelAttributeToDataProviderAdapter->isInferredRelation())
209  {
210  return $this->resolveJoinsForInferredRelation($onTableAliasName, $canUseFromJoins);
211  }
212  elseif ($this->modelAttributeToDataProviderAdapter->isAttributeOnDifferentModel())
213  {
214  return $this->resolveJoinsForAttributeOnDifferentModel($onTableAliasName, $canUseFromJoins);
215  }
216  elseif ($this->modelAttributeToDataProviderAdapter->isRelation())
217  {
218  return $this->resolveJoinsForAttributeOnSameModelThatIsARelation($onTableAliasName);
219  }
220  else
221  {
222  return $this->resolveJoinsForAttributeOnSameModelThatIsNotARelation($onTableAliasName);
223  }
224  }
225 
231  protected function resolveJoinsForDerivedRelationViaCastedUpModel($onTableAliasName, $canUseFromJoins = true)
232  {
233  assert('is_string($onTableAliasName)');
234  assert('is_bool($canUseFromJoins)');
235  //First cast up
237  $onTableAliasName, $canUseFromJoins);
238  //Second build relation across to the opposing model
240  $onTableAliasName);
241  //Third cast down if necessary
242  if ($this->modelAttributeToDataProviderAdapter->isDerivedRelationViaCastedUpModelDifferentThanOpposingModelClassName())
243  {
244  $opposingRelationModelClassName = $this->modelAttributeToDataProviderAdapter->
245  getOpposingRelationModelClassName();
246  $derivedRelationModelClassName = $this->modelAttributeToDataProviderAdapter->
247  getDerivedRelationViaCastedUpModelClassName();
249  $opposingRelationModelClassName,
250  $derivedRelationModelClassName, $onTableAliasName);
251  }
252  return $onTableAliasName;
253  }
254 
260  protected function resolveJoinsForDerivedRelationViaCastedUpModelThatIsCastedUp($onTableAliasName, $canUseFromJoins = true)
261  {
262  $modelClassName = $this->modelAttributeToDataProviderAdapter->getModelClassName();
263  $attributeModelClassName = $this->modelAttributeToDataProviderAdapter->
264  getCastedUpModelClassNameForDerivedRelation();
265  if ($canUseFromJoins)
266  {
267  return $this->processFromJoinsForAttributeThatIsCastedUp($modelClassName, $attributeModelClassName);
268  }
269  else
270  {
271  return $this->processLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName);
272  }
273  }
274 
280  {
281  assert('is_string($onTableAliasName)');
282  $opposingRelationTableName = $this->modelAttributeToDataProviderAdapter->getOpposingRelationTableName();
283  $relationJoiningTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
284  $this->modelAttributeToDataProviderAdapter->getManyToManyTableNameForDerivedRelationViaCastedUpModel(),
285  "id",
286  $onTableAliasName,
287  self::resolveForeignKey($onTableAliasName));
288  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
289  $opposingRelationTableName,
290  self::resolveForeignKey($opposingRelationTableName),
291  $relationJoiningTableAliasName,
292  'id');
293  return $onTableAliasName;
294  }
295 
302  protected function resolveJoinsForInferredRelation($onTableAliasName, $canUseFromJoins = true)
303  {
304  assert('is_string($onTableAliasName)');
305  assert('is_bool($canUseFromJoins)');
306  //First cast up
307  $onTableAliasName = $this->resolveJoinsForInferredRelationThatIsCastedUp(
308  $onTableAliasName, $canUseFromJoins);
309  //Second build relation across to the opposing model
310  $onTableAliasName = $this->resolveJoinsForForARelationAttributeThatIsManyToMany($onTableAliasName);
311  //Casting down should always be necessary since that is the whole point of using a referred relation
312  $opposingRelationModelClassName = $this->modelAttributeToDataProviderAdapter->getRelationModelClassName();
313  if ($opposingRelationModelClassName != 'Item')
314  {
315  throw new NotImplementedException();
316  }
317  $inferredRelationModelClassName = $this->modelAttributeToDataProviderAdapter->getInferredRelationModelClassName();
319  $opposingRelationModelClassName,
320  $inferredRelationModelClassName, $onTableAliasName);
321  return $onTableAliasName;
322  }
323 
329  protected function resolveJoinsForInferredRelationThatIsCastedUp($onTableAliasName, $canUseFromJoins = true)
330  {
331  $modelClassName = $this->modelAttributeToDataProviderAdapter->getModelClassName();
332  $attributeModelClassName = $this->modelAttributeToDataProviderAdapter->getAttributeModelClassName();
333  if ($modelClassName == $attributeModelClassName)
334  {
335  return $onTableAliasName;
336  }
337  if ($canUseFromJoins)
338  {
339  return $this->processFromJoinsForAttributeThatIsCastedUp($modelClassName, $attributeModelClassName);
340  }
341  else
342  {
343  return $this->processLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName);
344  }
345  }
346 
351  protected function resolveAttributeModelClassNameWithCastingHintForCastingDown($attributeModelClassName)
352  {
353  assert('is_string($attributeModelClassName)');
354  if ($this->modelAttributeToDataProviderAdapter->getCastingHintModelClassNameForAttribute() != null)
355  {
356  return $this->modelAttributeToDataProviderAdapter->getCastingHintModelClassNameForAttribute();
357  }
358  return $attributeModelClassName;
359  }
360 
366  protected function resolveJoinsForAttributeOnDifferentModel($onTableAliasName, $canUseFromJoins = true)
367  {
368  assert('is_string($onTableAliasName)');
369  assert('is_bool($canUseFromJoins)');
370  if ($this->modelAttributeToDataProviderAdapter->isRelation())
371  {
372  return $this->resolveJoinsForAttributeOnDifferentModelThatIsARelation($onTableAliasName, $canUseFromJoins);
373  }
374  else
375  {
376  return $this->resolveJoinsForAttributeOnDifferentModelThatIsNotARelation($onTableAliasName,
377  $canUseFromJoins);
378  }
379  }
380 
385  protected function resolveJoinsForAttributeOnSameModelThatIsARelation($onTableAliasName)
386  {
387  assert('is_string($onTableAliasName)');
388  return $this->resolveLeftJoinsForARelationAttribute($onTableAliasName);
389  }
390 
395  protected function resolveJoinsForAttributeOnSameModelThatIsNotARelation($onTableAliasName)
396  {
397  assert('is_string($onTableAliasName)');
398  return $onTableAliasName;
399  }
400 
406  protected function resolveJoinsForAttributeOnDifferentModelThatIsARelation($onTableAliasName, $canUseFromJoins = true)
407  {
408  assert('is_string($onTableAliasName)');
409  assert('is_bool($canUseFromJoins)');
410  if ($canUseFromJoins)
411  {
412  $onTableAliasName = $this->addMixedInOrCastedUpFromJoinsForAttribute($onTableAliasName);
413  }
414  else
415  {
416  $onTableAliasName = $this->addMixedInOrCastedUpLeftJoinsForAttribute($onTableAliasName);
417  }
418  return $this->resolveLeftJoinsForARelationAttribute($onTableAliasName);
419  }
420 
425  protected function resolveLeftJoinsForARelationAttribute($onTableAliasName)
426  {
427  assert('is_string($onTableAliasName)');
428  return $this->addLeftJoinsForARelationAttribute($onTableAliasName);
429  }
430 
436  protected function
437  resolveJoinsForAttributeOnDifferentModelThatIsNotARelation($onTableAliasName, $canUseFromJoins = true)
438  {
439  assert('is_string($onTableAliasName)');
440  assert('is_bool($canUseFromJoins)');
441  if ($canUseFromJoins)
442  {
443  return $this->addMixedInOrCastedUpFromJoinsForAttribute($onTableAliasName);
444  }
445  else
446  {
447  return $this->addMixedInOrCastedUpLeftJoinsForAttribute($onTableAliasName);
448  }
449  }
450 
455  protected function addMixedInOrCastedUpFromJoinsForAttribute($onTableAliasName)
456  {
457  assert('is_string($onTableAliasName)');
458  if ($this->modelAttributeToDataProviderAdapter->isAttributeMixedIn())
459  {
460  return $this->addFromJoinsForAttributeThatIsMixedIn($onTableAliasName);
461  }
462  else
463  {
465  }
466  }
467 
472  protected function addMixedInOrCastedUpLeftJoinsForAttribute($onTableAliasName)
473  {
474  assert('is_string($onTableAliasName)');
475  if ($this->modelAttributeToDataProviderAdapter->isAttributeMixedIn())
476  {
477  return $this->addLeftJoinsForAttributeThatIsMixedIn($onTableAliasName);
478  }
479  else
480  {
481  return $this->addLeftJoinsForAttributeThatIsCastedUp($onTableAliasName);
482  }
483  }
484 
489  protected function addFromJoinsForAttributeThatIsMixedIn($onTableAliasName)
490  {
491  assert('is_string($onTableAliasName)');
492  $modelClassName = $this->modelAttributeToDataProviderAdapter->getModelClassName();
493  $attributeTableName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName();
494  if (!$this->joinTablesAdapter->isTableInFromTables($attributeTableName))
495  {
496  $onTableAliasName = $this->joinTablesAdapter->addFromTableAndGetAliasName(
497  $attributeTableName,
498  self::resolveForeignKey($attributeTableName),
499  $modelClassName::getTableName());
500  }
501  return $onTableAliasName;
502  }
503 
508  {
509  $modelClassName = $this->modelAttributeToDataProviderAdapter->getModelClassName();
510  $attributeModelClassName = $this->modelAttributeToDataProviderAdapter->getAttributeModelClassName();
511  return $this->processFromJoinsForAttributeThatIsCastedUp($modelClassName, $attributeModelClassName);
512  }
513 
518  protected function addLeftJoinsForAttributeThatIsMixedIn($onTableAliasName)
519  {
520  assert('is_string($onTableAliasName)');
521  $attributeTableName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName();
522  return $this->addLeftJoinForMixedInAttribute($onTableAliasName, $attributeTableName);
523  }
524 
530  protected function addLeftJoinForMixedInAttribute($onTableAliasName, $attributeTableName)
531  {
532  assert('is_string($onTableAliasName)');
533  assert('is_string($attributeTableName)');
534  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
535  $attributeTableName,
536  self::resolveForeignKey($attributeTableName),
537  $onTableAliasName);
538  return $onTableAliasName;
539  }
540 
545  protected function addLeftJoinsForAttributeThatIsCastedUp($onTableAliasName)
546  {
547  $modelClassName = $this->modelAttributeToDataProviderAdapter->getResolvedModelClassName();
548  $attributeModelClassName = $this->modelAttributeToDataProviderAdapter->getAttributeModelClassName();
549  return $this->resolveAndProcessLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName);
550  }
551 
557  protected function addLeftJoinsForARelationAttribute($onTableAliasName)
558  {
559  assert('is_string($onTableAliasName)');
560  if ($this->modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::MANY_MANY)
561  {
562  return $this->resolveJoinsForForARelationAttributeThatIsManyToMany($onTableAliasName);
563  }
564  elseif ($this->modelAttributeToDataProviderAdapter->isRelationTypeAHasManyVariant())
565  {
566  $onTableAliasName = $this->resolveJoinsForForARelationAttributeThatIsAHasManyVariant($onTableAliasName);
567  $this->resolveSettingDistinctForARelationAttributeThatIsHasMany();
568  return $onTableAliasName;
569  }
570  elseif ($this->modelAttributeToDataProviderAdapter->isRelationTypeAHasOneVariant())
571  {
572  return $this->resolveJoinsForForARelationAttributeThatIsAHasOne($onTableAliasName);
573  }
574  else
575  {
576  throw new NotSupportedException();
577  }
578  }
579 
580  protected function resolveSettingDistinctForARelationAttributeThatIsHasMany()
581  {
582  if ($this->modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::HAS_MANY)
583  {
584  $this->resolveSetToDistinct();
585  }
586  }
587 
588  protected function resolveSetToDistinct()
589  {
590  if ($this->setDistinct)
591  {
592  $this->joinTablesAdapter->setSelectDistinctToTrue();
593  }
594  }
595 
600  protected function resolveJoinsForForARelationAttributeThatIsManyToMany($onTableAliasName)
601  {
602  assert('is_string($onTableAliasName)');
603  $relationTableName = $this->modelAttributeToDataProviderAdapter->getRelationTableName();
604  $attributeTableName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName();
605  $relationJoiningTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
606  $this->modelAttributeToDataProviderAdapter->getManyToManyTableName(),
607  "id",
608  $onTableAliasName,
609  self::resolveForeignKey($attributeTableName));
610  //if this is not the id column, then add an additional left join.
611  if ($this->modelAttributeToDataProviderAdapter->getRelatedAttribute() != 'id')
612  {
613  $this->resolveSetToDistinct();
614  return $this->joinTablesAdapter->addLeftTableAndGetAliasName(
615  $relationTableName,
616  self::resolveForeignKey($relationTableName),
617  $relationJoiningTableAliasName,
618  'id');
619  }
620  else
621  {
622  return $relationJoiningTableAliasName;
623  }
624  }
625 
630  protected function resolveJoinsForForARelationAttributeThatIsAHasManyVariant($onTableAliasName)
631  {
632  assert('is_string($onTableAliasName)');
633  $onTableJoinIdName = 'id';
634  $tableJoinIdName = $this->modelAttributeToDataProviderAdapter->getColumnName();
635  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
636  $this->modelAttributeToDataProviderAdapter->getRelationTableName(),
637  $onTableJoinIdName,
638  $onTableAliasName,
639  $tableJoinIdName);
640  return $onTableAliasName;
641  }
642 
647  protected function resolveJoinsForForARelationAttributeThatIsAHasOne($onTableAliasName)
648  {
649  assert('is_string($onTableAliasName)');
650  $tableJoinIdName = 'id';
651  $onTableJoinIdName = $this->modelAttributeToDataProviderAdapter->getColumnName();
652  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
653  $this->modelAttributeToDataProviderAdapter->getRelationTableName(),
654  $onTableJoinIdName,
655  $onTableAliasName,
656  $tableJoinIdName);
657  return $onTableAliasName;
658  }
659 
667  $castedDownModelClassName,
668  $onTableAliasName)
669 
670  {
671  assert('is_string($modelClassName)');
672  assert('is_string($castedDownModelClassName)');
673  assert('is_string($onTableAliasName)');
674  $resolvedCastedDownModelClassName = $this->resolveAttributeModelClassNameWithCastingHintForCastingDown(
675  $castedDownModelClassName);
676  if ($modelClassName != $resolvedCastedDownModelClassName)
677  {
678  //If the resolvedCastedDownModelClassName is actually casted up
679  $modelDerivationPathToItem = $this->resolveModelDerivationPathToItemForCastingDown(
680  $modelClassName, $resolvedCastedDownModelClassName);
681  if (empty($modelDerivationPathToItem))
682  {
683  return $this->processLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName,
684  $resolvedCastedDownModelClassName);
685  }
686  else
687  {
688  return $this->processLeftJoinsForAttributeThatIsCastedDown($modelClassName,
689  $resolvedCastedDownModelClassName,
690  $onTableAliasName);
691  }
692  }
693  return $onTableAliasName;
694  }
695 
702  protected function processLeftJoinsForAttributeThatIsCastedDown($modelClassName, $castedDownModelClassName,
703  $onTableAliasName)
704  {
705  assert('is_string($modelClassName)');
706  assert('is_string($castedDownModelClassName)');
707  assert('is_string($onTableAliasName)');
708  $modelDerivationPathToItem = $this->resolveModelDerivationPathToItemForCastingDown($modelClassName, $castedDownModelClassName);
709  foreach ($modelDerivationPathToItem as $modelClassNameToCastDownTo)
710  {
711  if ($modelClassNameToCastDownTo::getCanHaveBean())
712  {
713  $castedDownTableName = $modelClassNameToCastDownTo::getTableName();
714  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
715  $castedDownTableName,
716  'id',
717  $onTableAliasName,
718  self::resolveForeignKey($modelClassName::getTableName()));
719  $modelClassName = $modelClassNameToCastDownTo;
720  }
721  }
722  return $onTableAliasName;
723  }
724 
730  protected function resolveModelDerivationPathToItemForCastingDown($modelClassName, $castedDownModelClassName)
731  {
732  assert('is_string($modelClassName)');
733  assert('is_string($castedDownModelClassName)');
734  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($castedDownModelClassName);
735  if ($modelClassName == 'Item')
736  {
737  return $modelDerivationPathToItem;
738  }
739  foreach ($modelDerivationPathToItem as $key => $modelClassNameToCastDown)
740  {
741  unset($modelDerivationPathToItem[$key]);
742  if ($modelClassName == $modelClassNameToCastDown)
743  {
744  break;
745  }
746  }
747  return $modelDerivationPathToItem;
748  }
749 
756  protected static function resolveModelClassNameThatCanHaveTable($modelClassName, $castedDownModelClassName)
757  {
758  assert('is_string($modelClassName)');
759  assert('is_string($castedDownModelClassName)');
760  if (!$modelClassName::getCanHaveBean())
761  {
762  if (!$castedDownModelClassName::getCanHaveBean())
763  {
764  throw new NotSupportedException();
765  }
766  return $castedDownModelClassName;
767  }
768  else
769  {
770  return $modelClassName;
771  }
772  }
773 
780  private function processFromJoinsForAttributeThatIsCastedUp($modelClassName, $attributeModelClassName)
781  {
782  assert('is_string($modelClassName)');
783  assert('is_string($attributeModelClassName)');
784  $attributeTableName = $attributeModelClassName::getTableName();
785  $tableAliasName = $attributeTableName;
786  $castedDownModelClassName = $modelClassName;
787  $onTableAliasName = null;
788  while (get_parent_class($modelClassName) != $attributeModelClassName &&
789  get_parent_class($modelClassName) != 'RedBeanModel')
790  {
791  $castedDownFurtherModelClassName = $castedDownModelClassName;
792  $castedDownModelClassName = $modelClassName;
793  $modelClassName = get_parent_class($modelClassName);
794  if ($modelClassName::getCanHaveBean())
795  {
796  $castedUpAttributeTableName = $modelClassName::getTableName();
797  if (!$this->joinTablesAdapter->isTableInFromTables($castedUpAttributeTableName))
798  {
799  if ($onTableAliasName == null && $castedDownModelClassName::getCanHaveBean())
800  {
801  $onTableAliasName = $castedDownModelClassName::getTableName();
802  }
803  elseif ($onTableAliasName == null && $castedDownFurtherModelClassName::getCanHaveBean())
804  {
805  $onTableAliasName = $castedDownFurtherModelClassName::getTableName();
806  }
807  elseif ($onTableAliasName == null)
808  {
809  throw new NotSupportedException();
810  }
811  $onTableAliasName = $this->joinTablesAdapter->addFromTableAndGetAliasName(
812  $castedUpAttributeTableName,
813  self::resolveForeignKey($castedUpAttributeTableName),
814  $onTableAliasName);
815  }
816  }
817  }
818  if (!$this->joinTablesAdapter->isTableInFromTables($attributeTableName))
819  {
820  if ($onTableAliasName == null)
821  {
822  $modelClassName = static::resolveModelClassNameThatCanHaveTable($modelClassName, $castedDownModelClassName);
823  $onTableAliasName = $modelClassName::getTableName();
824  }
825  $tableAliasName = $this->joinTablesAdapter->addFromTableAndGetAliasName(
826  $attributeTableName, self::resolveForeignKey($attributeTableName), $onTableAliasName);
827  }
828  else
829  {
830  $existingTableAliasName = $this->joinTablesAdapter->getAlreadyFromJoinedTableAliasName($attributeTableName);
831  if ($existingTableAliasName != null)
832  {
833  $tableAliasName = $existingTableAliasName;
834  }
835  }
836  return $tableAliasName;
837  }
838 
845  private function resolveAndProcessLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName)
846  {
847  assert('is_string($onTableAliasName)');
848  assert('is_string($modelClassName)');
849  assert('is_string($attributeModelClassName)');
850  if ($modelClassName == $attributeModelClassName)
851  {
852  return $onTableAliasName;
853  }
854  return $this->processLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName);
855  }
856 
864  private function processLeftJoinsForAttributeThatIsCastedUp($onTableAliasName, $modelClassName, $attributeModelClassName)
865  {
866  assert('is_string($onTableAliasName)');
867  assert('is_string($modelClassName)');
868  assert('is_string($attributeModelClassName)');
869  $attributeTableName = $attributeModelClassName::getTableName();
870  $castedDownModelClassName = $modelClassName;
871  while (get_parent_class($modelClassName) != $attributeModelClassName &&
872  get_parent_class($modelClassName) != 'RedBeanModel')
873  {
874  $castedDownFurtherModelClassName = $castedDownModelClassName;
875  $castedDownModelClassName = $modelClassName;
876  $modelClassName = get_parent_class($modelClassName);
877  if ($modelClassName::getCanHaveBean())
878  {
879  $castedUpAttributeTableName = $modelClassName::getTableName();
880 
895  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
896  $castedUpAttributeTableName,
897  self::resolveForeignKey($castedUpAttributeTableName),
898  $onTableAliasName);//,
899  //$resolvedTableJoinIdName);
900  }
901  }
902  //Add left table if it is not already added
903  $modelClassName = static::resolveModelClassNameThatCanHaveTable($modelClassName, $castedDownModelClassName);
904  $onTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName(
905  $attributeTableName,
906  self::resolveForeignKey($attributeTableName),
907  $onTableAliasName); //,
908  //$modelClassName::getTableName());
909  return $onTableAliasName;
910  }
911 
915  private function resolveOnTableAliasNameForDerivedRelationViaCastedUpModel()
916  {
917  if ($this->modelAttributeToDataProviderAdapter->isAttributeDerivedRelationViaCastedUpModel())
918  {
919  $onTableAliasName = $this->modelAttributeToDataProviderAdapter->getModelTableName();
920  }
921  else
922  {
923  $onTableAliasName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName();
924  }
925  return $onTableAliasName;
926  }
927  }
928 ?>
static resolveModelClassNameThatCanHaveTable($modelClassName, $castedDownModelClassName)
resolveJoinsForDerivedRelationViaCastedUpModelThatIsCastedUp($onTableAliasName, $canUseFromJoins=true)
resolveJoinsForForARelationAttributeThatIsManyToMany($onTableAliasName)
resolveAndProcessLeftJoinsForAttributeThatIsCastedDownOrUp($modelClassName, $castedDownModelClassName, $onTableAliasName)
resolveJoinsForAttributeOnDifferentModelThatIsNotARelation($onTableAliasName, $canUseFromJoins=true)
resolveJoinsForAttributeOnSameModelThatIsNotARelation($onTableAliasName)
resolveJoinsForDerivedRelationViaCastedUpModelThatIsManyToMany($onTableAliasName)
resolveJoinsForForARelationAttributeThatIsAHasOne($onTableAliasName)
resolveJoinsForAttributeOnDifferentModelThatIsARelation($onTableAliasName, $canUseFromJoins=true)
resolveOnTableAliasName($onTableAliasName=null)
static resolveForeignKey($idName)
static makeColumnNameWithTableAlias($tableAliasName, $columnName)
addMixedInOrCastedUpFromJoinsForAttribute($onTableAliasName)
resolveJoinsForInferredRelation($onTableAliasName, $canUseFromJoins=true)
resolveJoinsForAttributeOnDifferentModel($onTableAliasName, $canUseFromJoins=true)
resolveJoinsForForARelationAttributeThatIsAHasManyVariant($onTableAliasName)
addLeftJoinsForAttributeThatIsCastedUp($onTableAliasName)
addMixedInOrCastedUpLeftJoinsForAttribute($onTableAliasName)
__construct(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $setDistinct=false)
addLeftJoinsForAttributeThatIsMixedIn($onTableAliasName)
addFromJoinsForAttributeThatIsMixedIn($onTableAliasName)
resolveLeftJoinsForARelationAttribute($onTableAliasName)
const MANY_MANY
Definition: BeanModel.php:78
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
resolveJoinsForAttributeOnSameModelThatIsARelation($onTableAliasName)
resolveJoinsForAttribute($onTableAliasName, $canUseFromJoins=true)
const HAS_MANY
Definition: BeanModel.php:71
resolveJoins($onTableAliasName=null, $canUseFromJoins=true)
addLeftJoinsForARelationAttribute($onTableAliasName)
resolveAttributeModelClassNameWithCastingHintForCastingDown($attributeModelClassName)
resolveModelDerivationPathToItemForCastingDown($modelClassName, $castedDownModelClassName)
resolveOnlyAttributeJoins($onTableAliasName=null, $canUseFromJoins=true)
addLeftJoinForMixedInAttribute($onTableAliasName, $attributeTableName)
processLeftJoinsForAttributeThatIsCastedDown($modelClassName, $castedDownModelClassName, $onTableAliasName)
resolveJoinsForDerivedRelationViaCastedUpModel($onTableAliasName, $canUseFromJoins=true)
resolveJoinsForInferredRelationThatIsCastedUp($onTableAliasName, $canUseFromJoins=true)
Generated on Sat Jul 4 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.