/**
  * @param string $relation
  * @return bool
  */
 public function relationIsReportedAsAttribute($relation)
 {
     assert('is_string($relation)');
     if ($this->model->isAttribute($relation) && $this->isReportedOnAsARelation($relation)) {
         return false;
     }
     if ($this->model->isAttribute($relation) && !$this->model->isRelation($relation)) {
         return false;
     }
     if ($this->isDerivedAttribute($relation)) {
         return false;
     }
     return $this->rules->relationIsReportedAsAttribute($this->model, $relation);
 }
 /**
  * @param string $relation
  * @param null|RedBeanModel $precedingModel
  * @param null|string $precedingRelation
  * @return bool
  */
 protected function relationLinksToPrecedingRelation($relation, RedBeanModel $precedingModel = null, $precedingRelation = null)
 {
     assert('is_string($relation)');
     if ($precedingModel == null || $precedingRelation == null) {
         return false;
     }
     //Check if the relation is a derived relation in which case return false because it is handled by
     //@see self::inferredRelationLinksToPrecedingRelation
     if (!$precedingModel->isAttribute($precedingRelation)) {
         return false;
     }
     if (get_class($precedingModel) != $this->model->getRelationmodelClassName($relation)) {
         return false;
     }
     if ($precedingModel->getRelationLinkType($precedingRelation) == RedBeanModel::LINK_TYPE_ASSUMPTIVE && $this->model->getRelationLinkType($relation) == RedBeanModel::LINK_TYPE_ASSUMPTIVE) {
         return true;
     }
     //Check for LINK_TYPE_SPECIFIC
     if ($precedingModel->getRelationLinkType($precedingRelation) == RedBeanModel::LINK_TYPE_SPECIFIC && $this->model->getRelationLinkType($relation) == RedBeanModel::LINK_TYPE_SPECIFIC && $precedingModel->getRelationLinkName($precedingRelation) == $this->model->getRelationLinkName($relation)) {
         return true;
     }
     return false;
 }
 /**
  * @param RedBeanModel $model
  * @param array $modelRelations
  * @return bool
  * @throws ApiException
  */
 protected function manageModelRelations($model, $modelRelations)
 {
     try {
         if (isset($modelRelations) && !empty($modelRelations)) {
             foreach ($modelRelations as $modelRelation => $relations) {
                 if ($model->isAttribute($modelRelation) && ($model->getRelationType($modelRelation) == RedBeanModel::HAS_MANY || $model->getRelationType($modelRelation) == RedBeanModel::MANY_MANY)) {
                     foreach ($relations as $relation) {
                         $relatedModelClassName = $relation['modelClassName'];
                         try {
                             $relatedModel = $relatedModelClassName::getById(intval($relation['modelId']));
                         } catch (Exception $e) {
                             $message = Zurmo::t('ZurmoModule', 'The related model ID specified was invalid.');
                             throw new NotFoundException($message);
                         }
                         if ($relation['action'] == 'add') {
                             $model->{$modelRelation}->add($relatedModel);
                         } elseif ($relation['action'] == 'remove') {
                             $model->{$modelRelation}->remove($relatedModel);
                         } else {
                             $message = Zurmo::t('ZurmoModule', 'Unsupported action.');
                             throw new NotSupportedException($message);
                         }
                     }
                 } else {
                     $message = Zurmo::t('ZurmoModule', 'You can add relations only for HAS_MANY and MANY_MANY relations.');
                     throw new NotSupportedException($message);
                 }
             }
         }
     } catch (Exception $e) {
         $message = $e->getMessage();
         throw new ApiException($message);
     }
     return true;
 }
Пример #4
0
 /**
  * @param RedBeanModel $model
  * @param string $attribute
  * @param DisplayAttributeForReportForm $displayAttribute
  * @return mixedg $value
  * @throws NotSupportedException
  */
 protected function resolveModelAttributeValueForPenultimateRelation(RedBeanModel $model, $attribute, DisplayAttributeForReportForm $displayAttribute)
 {
     if ($model->isAttribute($attribute)) {
         return $model->{$attribute};
     }
     $penultimateRelation = $displayAttribute->getPenultimateRelation();
     if (!$model->isAttribute($penultimateRelation)) {
         throw new NotSupportedException();
     }
     return $model->{$penultimateRelation}->{$attribute};
 }
Пример #5
0
 /**
  * The check for OwnedModel below, is because it is possible that something like Address 'state' would be coming
  * into this method for retrieval. but depending how things are called, it might need to use the penultimate
  * relation instead of looking for the attribute on the $model.
  * @param RedBeanModel $model
  * @param string $attribute
  * @param DisplayAttributeForReportForm $displayAttribute
  * @return mixedg $value
  * @throws NotSupportedException
  */
 protected function resolveModelAttributeValueForPenultimateRelation(RedBeanModel $model, $attribute, DisplayAttributeForReportForm $displayAttribute)
 {
     if ($model->isAttribute($attribute) && !is_subclass_of($displayAttribute->getResolvedAttributeModelClassName(), 'OwnedModel')) {
         return $model->{$attribute};
     }
     $penultimateRelation = $displayAttribute->getPenultimateRelation();
     if (!$model->isAttribute($penultimateRelation)) {
         //Fallback. For some reason this could in fact reference the original $model attribute, but because it is
         //not owned, it gets confused. todo: need to investigate this further because this is not proper.
         if ($model->isAttribute($attribute)) {
             return $model->{$attribute};
         }
         throw new NotSupportedException();
     }
     return $model->{$penultimateRelation}->{$attribute};
 }