/** * Magic Getter. Gets the value for a specific property in the bean. * If the property does not exist this getter will make sure no error * occurs. This is because RedBean allows you to query (probe) for * properties. If the property can not be found this method will * return NULL instead. * @param string $property * @return mixed $value */ public function &__get($property) { if ($this->beanHelper) { $toolbox = $this->beanHelper->getToolbox(); } if (!isset($this->properties[$property])) { $fieldLink = $property . "_id"; /** * All this magic can be become very complex quicly. For instance, * my PHP CLI produced a segfault while testing this code. Turns out that * if fieldlink equals idfield, scripts tend to recusrively load beans and * instead of giving a clue they simply crash and burn isnt that nice? */ if (isset($this->{$fieldLink}) && $fieldLink != $this->getMeta('sys.idfield')) { $this->setMeta("tainted", true); $type = $toolbox->getWriter()->getAlias($property); $targetType = $this->properties[$fieldLink]; $bean = $toolbox->getRedBean()->load($type, $targetType); //return $bean; $this->properties[$property] = $bean; return $this->properties[$property]; } if (strpos($property, 'own') === 0) { $firstCharCode = ord(substr($property, 3, 1)); if ($firstCharCode >= 65 && $firstCharCode <= 90) { $type = lcfirst(str_replace('own', '', $property)); $myFieldLink = $this->getMeta('type') . "_id"; $beans = $toolbox->getRedBean()->find($type, array(), array(" {$myFieldLink} = ? ", array($this->getID()))); $this->properties[$property] = $beans; $this->setMeta("sys.shadow." . $property, $beans); $this->setMeta("tainted", true); return $this->properties[$property]; } } if (strpos($property, 'shared') === 0) { $firstCharCode = ord(substr($property, 6, 1)); if ($firstCharCode >= 65 && $firstCharCode <= 90) { $type = lcfirst(str_replace('shared', '', $property)); $keys = $toolbox->getRedBean()->getAssociationManager()->related($this, $type); if (!count($keys)) { $beans = array(); } else { $beans = $toolbox->getRedBean()->batch($type, $keys); } $this->properties[$property] = $beans; $this->setMeta("sys.shadow." . $property, $beans); $this->setMeta("tainted", true); return $this->properties[$property]; } } return $this->null; } return $this->properties[$property]; }
/** * Counts all shared beans of type $type. * Also works with via(), with() and withCondition(). * * @param string $type type of bean you wish to count * * @return integer */ public function countShared($type) { $toolbox = $this->beanHelper->getToolbox(); $redbean = $toolbox->getRedBean(); $writer = $toolbox->getWriter(); if ($this->via) { $oldName = $writer->getAssocTable(array($this->__info['type'], $type)); if ($oldName !== $this->via) { //set the new renaming rule $writer->renameAssocTable($oldName, $this->via); $this->via = NULL; } } $type = $this->beau($type); $count = 0; if ($this->getID() > 0) { $count = $redbean->getAssociationManager()->relatedCount($this, $type, $this->withSql, $this->withParams, TRUE); } $this->withSql = ''; $this->withParams = array(); return (int) $count; }
/** * Magic Getter. Gets the value for a specific property in the bean. * If the property does not exist this getter will make sure no error * occurs. This is because RedBean allows you to query (probe) for * properties. If the property can not be found this method will * return NULL instead. * @param string $property * @return mixed $value */ public function &__get($property) { if (self::$flagUseBeautyfulColumnnames && !$this->flagSkipBeau) { $property = $this->beau($property); } if ($this->beanHelper) { $toolbox = $this->beanHelper->getToolbox(); $redbean = $toolbox->getRedBean(); } if ($this->withSql !== '') { if (strpos($property, 'own') === 0) { unset($this->properties[$property]); } } if (!isset($this->properties[$property])) { $fieldLink = $property . '_id'; if (isset($this->{$fieldLink}) && $fieldLink !== $this->getMeta('sys.idfield')) { $this->__info['tainted'] = true; $bean = $this->getMeta('sys.parentcache.' . $property); if (!$bean) { $type = $this->getAlias($property); $targetType = $this->properties[$fieldLink]; $bean = $redbean->load($type, $targetType); } $this->properties[$property] = $bean; return $this->properties[$property]; } elseif (strpos($property, 'own') === 0 && ctype_upper(substr($property, 3, 1))) { $type = lcfirst(substr($property, 3)); if (self::$flagUseBeautyfulColumnnames) { $type = $this->beau($type); } if ($this->aliasName) { $parentField = $this->aliasName; $myFieldLink = $this->aliasName . '_id'; $this->__info['sys.alias.' . $type] = $this->aliasName; $this->aliasName = null; } else { $myFieldLink = $this->__info['type'] . '_id'; $parentField = $this->__info['type']; } $beans = array(); if ($this->getID() > 0) { $params = array_merge(array($this->getID()), $this->withParams); $beans = $redbean->find($type, array(), array(" {$myFieldLink} = ? " . $this->withSql, $params)); } $this->withSql = ''; $this->withParams = array(); foreach ($beans as $b) { $b->__info['sys.parentcache.' . $parentField] = $this; } $this->properties[$property] = $beans; $this->__info['sys.shadow.' . $property] = $beans; $this->__info['tainted'] = true; return $this->properties[$property]; } elseif (strpos($property, 'shared') === 0 && ctype_upper(substr($property, 6, 1))) { $type = lcfirst(substr($property, 6)); if (self::$flagUseBeautyfulColumnnames) { $type = $this->beau($type); } $keys = $redbean->getAssociationManager()->related($this, $type); if (!count($keys)) { $beans = array(); } else { if (trim($this->withSql) !== '') { $beans = $redbean->find($type, array('id' => $keys), array($this->withSql, $this->withParams), true); } else { $beans = $redbean->batch($type, $keys); } } $this->withSql = ''; $this->withParams = array(); $this->properties[$property] = $beans; $this->__info['sys.shadow.' . $property] = $beans; $this->__info['tainted'] = true; return $this->properties[$property]; } else { $null = null; return $null; } } else { return $this->properties[$property]; } }