/**
  * Notice the use of $modelToForgetCache. This was needed to avoid a caching issue with the following example.
  * If an opportunity fires, and a related account's opportunity is created. This new opportunity had a cached
  * model for account that was null.  So this is fixed by forgetting the new model after it is added to the account.
  * @throws FailedToSaveModelException
  * @throws NotSupportedException
  */
 protected function processCreateRelatedAction()
 {
     if ($this->action->relationFilter != ActionForWorkflowForm::RELATION_FILTER_ALL) {
         throw new NotSupportedException();
     }
     $modelClassName = get_class($this->triggeredModel);
     if ($this->triggeredModel->isADerivedRelationViaCastedUpModel($this->action->relation) && $this->triggeredModel->getDerivedRelationType($this->action->relation) == RedBeanModel::MANY_MANY) {
         foreach (WorkflowUtil::resolveDerivedModels($this->triggeredModel, $this->action->relation) as $relatedModel) {
             if ($this->resolveCreateModel($relatedModel, $this->action->relatedModelRelation)) {
                 $saved = $relatedModel->save();
                 if (!$saved) {
                     throw new FailedToSaveModelException();
                 }
             }
         }
     } elseif ($this->triggeredModel->getInferredRelationModelClassNamesForRelation(ModelRelationsAndAttributesToWorkflowAdapter::resolveRealAttributeName($this->action->relation)) != null) {
         foreach (WorkflowUtil::getInferredModelsByAtrributeAndModel($this->action->relation, $this->triggeredModel) as $relatedModel) {
             if ($this->resolveCreateModel($relatedModel, $this->action->relatedModelRelation)) {
                 $saved = $relatedModel->save();
                 if (!$saved) {
                     throw new FailedToSaveModelException();
                 }
             }
         }
     } elseif ($this->triggeredModel->{$this->action->relation} instanceof RedBeanMutableRelatedModels) {
         foreach ($this->triggeredModel->{$this->action->relation} as $relatedModel) {
             if ($this->resolveCreateModel($relatedModel, $this->action->relatedModelRelation)) {
                 $saved = $relatedModel->save();
                 if (!$saved) {
                     throw new FailedToSaveModelException();
                 }
             }
         }
     } elseif ($modelClassName::isRelationTypeAHasOneVariant($this->action->relation) && !$modelClassName::isOwnedRelation($this->action->relation)) {
         $relatedModel = $this->triggeredModel->{$this->action->relation};
         $modelToForgetCache = null;
         if ($this->resolveCreateModel($relatedModel, $this->action->relatedModelRelation, $modelToForgetCache)) {
             $saved = $relatedModel->save();
             if (!$saved) {
                 throw new FailedToSaveModelException();
             }
             if ($modelToForgetCache instanceof RedBeanModel) {
                 $modelToForgetCache->forget();
             }
         }
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * @param $relation
  * @return null|string
  */
 protected function getInferredRelationModelClassNamesForRelation($relation)
 {
     assert('is_string($relation)');
     return $this->model->getInferredRelationModelClassNamesForRelation($relation);
 }