Beispiel #1
0
 /**
  * Used by classes such as containers which use sql to
  * optimize getting models from the database.
  */
 public static function getForeignKeyName($modelClassName, $relationName)
 {
     assert('is_string($modelClassName)');
     assert('$modelClassName != ""');
     $metadata = $modelClassName::getMetadata();
     foreach ($metadata as $modelClassName => $modelClassMetadata) {
         if (isset($metadata[$modelClassName]["relations"]) && array_key_exists($relationName, $metadata[$modelClassName]["relations"])) {
             $relatedModelClassName = $metadata[$modelClassName]['relations'][$relationName][1];
             self::resolveModelClassNameForClassesWithoutBeans($relatedModelClassName);
             $relatedModelTableName = self::getTableName($relatedModelClassName);
             $linkType = null;
             $relationLinkName = null;
             self::resolveLinkTypeAndRelationLinkName($metadata[$modelClassName]['relations'][$relationName], $linkType, $relationLinkName);
             $linkName = self::makeCasedLinkName($metadata[$modelClassName]['relations'][$relationName][0], $linkType, $relationLinkName);
             if ($metadata[$modelClassName]['relations'][$relationName][0] == self::HAS_MANY || $metadata[$modelClassName]['relations'][$relationName][0] == self::HAS_ONE_BELONGS_TO) {
                 $columnName = self::getTableName($modelClassName) . '_id';
             } else {
                 $columnName = $relatedModelTableName . '_id';
             }
             $columnName = ZurmoRedBeanLinkManager::resolveColumnPrefix($linkName) . $columnName;
             return $columnName;
         }
     }
     throw new NotSupportedException();
 }
 public function save($runValidation = true)
 {
     if (!parent::save($runValidation)) {
         return false;
     }
     foreach ($this->deferredRelateBeans as $bean) {
         if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC) {
             if ($this->bean->id == null) {
                 R::store($this->bean);
             }
             $polyIdFieldName = strtolower($this->linkName) . '_id';
             $polyTypeFieldName = strtolower($this->linkName) . '_type';
             $bean->{$polyTypeFieldName} = $this->bean->getMeta('type');
             $bean->{$polyIdFieldName} = $this->bean->id;
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = RedBeanModel::getTableName($this->modelClassName);
                 RedBeanColumnTypeOptimizer::optimize($tableName, $polyIdFieldName, 'id');
             }
         } else {
             ZurmoRedBeanLinkManager::link($bean, $this->bean, $this->resolveLinkNameForCasing());
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = RedBeanModel::getTableName($this->modelClassName);
                 $columnName = RedBeanModel::getTableName($this->relatedModelClassName) . '_id';
                 $columnName = ZurmoRedBeanLinkManager::resolveColumnPrefix($this->resolveLinkNameForCasing()) . $columnName;
                 RedBeanColumnTypeOptimizer::optimize($tableName, $columnName, 'id');
             }
         }
         R::store($bean);
     }
     $this->deferredRelateBeans = array();
     $tableName = RedBeanModel::getTableName($this->relatedModelClassName);
     foreach ($this->deferredUnrelateBeans as $bean) {
         if (!$this->owns) {
             ZurmoRedBeanLinkManager::breakLink($bean, $tableName, $this->resolveLinkNameForCasing());
             R::store($bean);
         } else {
             R::trash($bean);
         }
     }
     $this->deferredUnrelateBeans = array();
     return true;
 }