/** * Dispenses a new RedBean OODB Bean for use with * the rest of the methods. * * @param string|array $typeOrBeanArray type or bean array to import * @param integer $number number of beans to dispense * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * * @return array|OODBBean */ public static function dispense($typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE) { if (is_array($typeOrBeanArray)) { if (!isset($typeOrBeanArray['_type'])) { $list = array(); foreach ($typeOrBeanArray as $beanArray) { if (!(is_array($beanArray) && isset($beanArray['_type']))) { throw new RedException('Invalid Array Bean'); } } foreach ($typeOrBeanArray as $beanArray) { $list[] = self::dispense($beanArray); } return $list; } $import = $typeOrBeanArray; $type = $import['_type']; unset($import['_type']); } else { $type = $typeOrBeanArray; } if (!preg_match('/^[a-z0-9]+$/', $type)) { throw new RedException('Invalid type: ' . $type); } $beanOrBeans = self::$redbean->dispense($type, $num, $alwaysReturnArray); if (isset($import)) { $beanOrBeans->import($import); } return $beanOrBeans; }
/** * Creates a copy of bean $bean and copies all primitive properties (not lists) * and the parents beans to the newly created bean. Also sets the ID of the bean * to 0. * * @param OODBBean $bean bean to copy * * @return OODBBean */ private function createCopy(OODBBean $bean) { $type = $bean->getMeta('type'); $copy = $this->redbean->dispense($type); $copy->importFrom($bean); $copy->id = 0; return $copy; }
/** * @see Finder::find * Convience method. Tries to find beans of a certain type, * if no beans are found, it dispenses a bean of that type. * * @param string $type the type of bean you are looking for * @param string $sql SQL query to find the desired bean, starting right after WHERE clause * @param array $bindings values array of values to be bound to parameters in query * * @return array */ public function findOrDispense($type, $sql = NULL, $bindings = array()) { $foundBeans = $this->find($type, $sql, $bindings); if (empty($foundBeans)) { return array($this->redbean->dispense($type)); } else { return $foundBeans; } }
/** * Finds or creates a bean. * Tries to find a bean with certain properties specified in the second * parameter ($like). If the bean is found, it will be returned. * If multiple beans are found, only the first will be returned. * If no beans match the criteria, a new bean will be dispensed, * the criteria will be imported as properties and this new bean * will be stored and returned. * * Format of criteria set: property => value * The criteria set also supports OR-conditions: property => array( value1, orValue2 ) * * @param string $type type of bean to search for * @param array $like criteria set describing bean to search for * * @return OODBBean */ public function findOrCreate($type, $like = array()) { $beans = $this->findLike($type, $like); if (count($beans)) { $bean = reset($beans); return $bean; } $bean = $this->redbean->dispense($type); $bean->import($like); $this->redbean->store($bean); return $bean; }
/** * Associates two beans in a many-to-many relation. * This method will associate two beans and store the connection between the * two in a link table. Instead of two single beans this method also accepts * two sets of beans. Returns the ID or the IDs of the linking beans. * * @param OODBBean|array $beans1 one or more beans to form the association * @param OODBBean|array $beans2 one or more beans to form the association * * @return array */ public function associate($beans1, $beans2) { if (!is_array($beans1)) { $beans1 = array($beans1); } if (!is_array($beans2)) { $beans2 = array($beans2); } $results = array(); foreach ($beans1 as $bean1) { foreach ($beans2 as $bean2) { $table = $this->getTable(array($bean1->getMeta('type'), $bean2->getMeta('type'))); $bean = $this->oodb->dispense($table); $results[] = $this->associateBeans($bean1, $bean2, $bean); } } return count($results) > 1 ? $results : reset($results); }
/** * Dispenses a new RedBean OODB Bean for use with * the rest of the methods. * * @param string|array $typeOrBeanArray type or bean array to import * @param integer $number number of beans to dispense * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * * @return array|OODBBean * * @throws Security */ public static function dispense($typeOrBeanArray, $num = 1, $alwaysReturnArray = FALSE) { if (is_array($typeOrBeanArray)) { if (!isset($typeOrBeanArray['_type'])) { throw new RedException('Missing _type field.'); } $import = $typeOrBeanArray; $type = $import['_type']; unset($import['_type']); } else { $type = $typeOrBeanArray; } if (!preg_match('/^[a-z0-9]+$/', $type)) { throw new RedException('Invalid type: ' . $type); } $beanOrBeans = self::$redbean->dispense($type, $num, $alwaysReturnArray); if (isset($import)) { $beanOrBeans->import($import); } return $beanOrBeans; }