protected function constructDerived($bean, $setDefaults) { assert('$bean === null || $bean instanceof RedBean_OODBBean'); assert('is_bool($setDefaults)'); // Does a subset of what RedBeanModel::__construct does // in order to mix in the Person - this is metadata wise, // User doesn't get any functionality from Person. $modelClassName = 'Person'; $tableName = self::getTableName($modelClassName); if ($bean === null) { $personBean = R::dispense($tableName); } else { $userBean = $this->getClassBean('User'); $personBean = ZurmoRedBeanLinkManager::getBean($userBean, $tableName); assert('$personBean !== null'); } $this->setClassBean($modelClassName, $personBean); $this->mapAndCacheMetadataAndSetHints($modelClassName, $personBean); parent::constructDerived($bean, $setDefaults); }
/** * A protected version of __get() for models to talk to themselves * to use their dynamically created members from 'members' * and 'relations' in its metadata. */ protected function unrestrictedGet($attributeName) { assert('is_string($attributeName)'); assert('$attributeName != ""'); assert("property_exists(\$this, '{$attributeName}') || \$this->isAttribute('{$attributeName}')"); if (property_exists($this, $attributeName)) { return $this->{$attributeName}; } elseif ($attributeName == 'id') { $id = intval($this->getPrimaryBean()->id); assert('$id >= 0'); if ($id == 0) { $id = $this->pseudoId; } return $id; } elseif ($this->isAttribute($attributeName)) { list($bean, $attributeModelClassName) = $this->attributeNameToBeanAndClassName[$attributeName]; if (!static::isRelation($attributeName)) { $columnName = strtolower($attributeName); return $bean->{$columnName}; } else { if (!array_key_exists($attributeName, $this->relationNameToRelatedModel)) { $relationAndOwns = static::getRelationNameToRelationTypeModelClassNameAndOwnsForModel(); list($relationType, $relatedModelClassName, $owns, $linkType, $relationLinkName) = $relationAndOwns[$attributeName]; $tempRelatedModelClassName = $relatedModelClassName; self::resolveModelClassNameForClassesWithoutBeans($tempRelatedModelClassName); $relatedTableName = self::getTableName($tempRelatedModelClassName); switch ($relationType) { case self::HAS_ONE_BELONGS_TO: $linkName = strtolower(get_class($this)); $columnName = $linkName . '_id'; $relatedBeans = R::find($relatedTableName, $columnName . " = " . $bean->id); if (count($relatedBeans) > 1) { throw new NotFoundException(); } elseif (count($relatedBeans) == 0) { $relatedModel = new $relatedModelClassName(); } else { $relatedModel = self::makeModel(end($relatedBeans), $relatedModelClassName); } $this->relationNameToRelatedModel[$attributeName] = $relatedModel; break; case self::HAS_ONE: case self::HAS_MANY_BELONGS_TO: $linkName = self::makeCasedLinkName($relationType, $linkType, $relationLinkName); if ($bean->id > 0 && !in_array($attributeName, $this->unlinkedRelationNames)) { $linkFieldName = ZurmoRedBeanLinkManager::getLinkField($relatedTableName, $linkName); if ((int) $bean->{$linkFieldName} > 0) { $beanIdentifier = $relatedTableName . (int) $bean->{$linkFieldName}; try { $relatedBean = RedBeansCache::getBean($beanIdentifier); } catch (NotFoundException $e) { $relatedBean = ZurmoRedBeanLinkManager::getBean($bean, $relatedTableName, $linkName); RedBeansCache::cacheBean($relatedBean, $beanIdentifier); } if ($relatedBean !== null && $relatedBean->id > 0) { $relatedModel = self::makeModel($relatedBean, $relatedModelClassName); } } } if (!isset($relatedModel)) { $relatedModel = new $relatedModelClassName(); } $this->relationNameToRelatedModel[$attributeName] = $relatedModel; break; case self::HAS_MANY: $this->relationNameToRelatedModel[$attributeName] = new RedBeanOneToManyRelatedModels($bean, $relatedModelClassName, $attributeModelClassName, $owns, $linkType, $relationLinkName); break; case self::MANY_MANY: $this->relationNameToRelatedModel[$attributeName] = new RedBeanManyToManyRelatedModels($bean, $relatedModelClassName, $linkType, $relationLinkName); break; default: throw new NotSupportedException(); } } return $this->relationNameToRelatedModel[$attributeName]; } } else { throw new NotSupportedException('Invalid Attribute: ' . $attributeName); } }
/** * @param RedBean_OODBBean $bean * @param bool $setDefaults */ protected function constructDerived($bean, $setDefaults) { assert('$bean === null || $bean instanceof RedBean_OODBBean'); assert('is_bool($setDefaults)'); // Does a subset of what RedBeanModel::__construct does // in order to mix in the Person - this is metadata wise, // User doesn't get any functionality from Person. $modelClassName = 'Person'; $tableName = $modelClassName::getTableName(); if ($bean === null) { $personBean = ZurmoRedBean::dispense($tableName); } else { $userBean = $this->getClassBean('User'); $personBean = ZurmoRedBeanLinkManager::getBean($userBean, $tableName); assert('$personBean !== null'); } //This is a hack to recover from a bug we cannot figure out how to solve. //Rarely the person attributes are not part of the user, memcache needs to be restarted to solve this //problem as you can't use the system once this occurs. this check below will clear the specific cache //that causes this. Still need to figure out what is setting the cache wrong to begin with if (!static::isAnAttribute('lastName')) { static::forgetBeanModel('User'); } $this->setClassBean($modelClassName, $personBean); $this->mapAndCacheMetadataAndSetHints($modelClassName, $personBean); parent::constructDerived($bean, $setDefaults); }