/** * Iterates through the specified own-list and * fetches all properties (with their type) and * returns the references. * Use this method to quickly load indirectly related * beans in an own-list. Whenever you cannot use a * shared-list this method offers the same convenience * by aggregating the parent beans of all children in * the specified own-list. * * Example: * * <code> * $quest->aggr( 'xownQuestTarget', 'target', 'quest' ); * </code> * * Loads (in batch) and returns references to all * quest beans residing in the $questTarget->target properties * of each element in the xownQuestTargetList. * * @param string $list the list you wish to process * @param string $property the property to load * @param string $type the type of bean residing in this property (optional) * * @return array */ public function &aggr($list, $property, $type = NULL) { $this->via = NULL; $ids = $beanIndex = $references = array(); if (strlen($list) < 4) { throw new RedException('Invalid own-list.'); } if (strpos($list, 'own') !== 0) { throw new RedException('Only own-lists can be aggregated.'); } if (!ctype_upper(substr($list, 3, 1))) { throw new RedException('Invalid own-list.'); } if (is_null($type)) { $type = $property; } foreach ($this->{$list} as $bean) { $field = $property . '_id'; if (isset($bean->{$field})) { $ids[] = $bean->{$field}; $beanIndex[$bean->{$field}] = $bean; } } $beans = $this->beanHelper->getToolBox()->getRedBean()->batch($type, $ids); //now preload the beans as well foreach ($beans as $bean) { $beanIndex[$bean->id]->setProperty($property, $bean); } foreach ($beanIndex as $indexedBean) { $references[] = $indexedBean->{$property}; } return $references; }
/** * 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->clearModifiers(); return (int) $count; }