Exemplo n.º 1
0
 /**
  * @param string $username
  * @throws NotFoundException
  */
 public static function getByUsername($username)
 {
     assert('is_string($username)');
     assert('$username != ""');
     $bean = ZurmoRedBean::findOne('_user', "username = :username ", array(':username' => $username));
     assert('$bean === false || $bean instanceof RedBean_OODBBean');
     if ($bean === false) {
         throw new NotFoundException();
     }
     RedBeansCache::cacheBean($bean, static::getTableName() . $bean->id);
     return self::makeModel($bean);
 }
Exemplo n.º 2
0
 /**
  * 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);
     }
 }