/** * Handles a REST POST request. * Stores the bean described in the payload array in the database. * Returns an array formatted according to RedBeanPHP REST BeanCan * formatting specifications. * * Format of the payload array: * * array( * 'bean' => array( property => value pairs ) * ) * * @return array */ private function post() { if (!isset($this->payload['bean'])) { return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Missing parameter \'bean\'.'); } if (!is_array($this->payload['bean'])) { return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Parameter \'bean\' must be object/array.'); } foreach ($this->payload['bean'] as $key => $value) { if (!is_string($key) || !is_string($value)) { return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Object \'bean\' invalid.'); } } $newBean = $this->oodb->dispense($this->type); $newBean->import($this->payload['bean']); if (strpos($this->list, 'shared-') === FALSE) { $listName = 'own' . ucfirst($this->list); } else { $listName = 'shared' . ucfirst(substr($this->list, 7)); } array_push($this->bean->{$listName}, $newBean); $this->oodb->store($this->bean); $newBean = $this->oodb->load($newBean->getMeta('type'), $newBean->id); return $this->resp($newBean->export()); }
/** * Tries to load a bean from cache, if this fails, it asks * the oodb object to load the bean from the database. * @param string $type * @param integer $id * @return RedBean_OODB $bean */ public function load($type, $id) { $bean = $this->fetchFromCacheByTypeID($type, $id); if ($bean) { return $bean; } else { $bean = $this->oodb->load($type, $id); $this->putInCache($bean); return $bean; } }
/** * Loads bean, recurses if one of the property appears to be a list. * * @param array $array data array to import as a bean * @param boolean $filterEmpty if TRUE empty STRING values are converted to NULL (default FALSE) * * @return RedBean_OODBBean * * @throws RedBean_Exception_Security */ private function loadBean(&$array, $filterEmpty) { $type = $array['type']; unset($array['type']); if (isset($array['id'])) { // Do we need to load the bean? if (self::$loadBeans) { $bean = $this->redbean->load($type, (int) $array['id']); } else { throw new RedBean_Exception_Security('Attempt to load a bean in Cooker. Use enableBeanLoading to override but please read security notices first.'); } } else { $bean = $this->redbean->dispense($type); } foreach ($array as $property => $value) { if (is_array($value)) { $bean->{$property} = $this->graph($value, $filterEmpty); } else { $bean->{$property} = $value == '' && self::$useNULLForEmptyString ? NULL : $value; } } return $bean; }
/** * Loads a bean by type and id. If the bean cannot be found an * empty bean will be returned instead. This is a cached version * of the loader, if the bean has been cached it will be served * from cache, otherwise the bean will be retrieved from the database * as usual an a new cache entry will be added.. * * @param string $type type of bean you are looking for * @param integer $id identifier of the bean * * @return RedBean_OODBBean $bean the bean object found */ public function load($type, $id) { if (isset($this->cache[$type][$id])) { $this->hits++; $bean = $this->cache[$type][$id]; } else { $this->misses++; $bean = parent::load($type, $id); if ($bean->id) { if (!isset($this->cache[$type])) { $this->cache[$type] = array(); } $this->cache[$type][$id] = $bean; } } return $bean; }
/** * Loads the bean with the given type and id and returns it. * * @param string $type type * @param integer $id id of the bean you want to load * * @return RedBean_OODBBean $bean */ public static function load($type, $id) { return self::$redbean->load($type, $id); }
/** * Loads the Bean internally * @param integer $id */ public function find($id) { $this->bean = $this->redbean->load($this->bean->getMeta("type"), (int) $id); }
} $writer2 = new MyWriter($adapter); $redbean2 = new RedBean_OODB($writer2); $movie = $redbean2->dispense("movie"); asrt(isset($movie->movie_id), true); $movie->name = "movie 1"; $movieid = $redbean2->store($movie); asrt($movieid > 0, true); $columns = array_keys($writer->getColumns("movie")); asrt(in_array("movie_id", $columns), true); asrt(in_array("id", $columns), false); $movie2 = $redbean2->dispense("movie"); asrt(isset($movie2->movie_id), true); $movie2->name = "movie 2"; $movieid2 = $redbean2->store($movie2); $movie1 = $redbean2->load("movie", $movieid); asrt($movie->name, "movie 1"); $movie2 = $redbean2->load("movie", $movieid2); asrt($movie2->name, "movie 2"); $movies = $redbean2->batch("movie", array($movieid, $movieid2)); asrt(count($movies), 2); asrt($movies[$movieid]->name, "movie 1"); asrt($movies[$movieid2]->name, "movie 2"); $toolbox2 = new RedBean_ToolBox($redbean2, $adapter, $writer2); $a2 = new RedBean_AssociationManager($toolbox2); $a2->associate($movie1, $movie2); $movies = $a2->related($movie1, "movie"); asrt(count($movies), 1); asrt((int) $movies[0], (int) $movieid2); $movies = $a2->related($movie2, "movie"); asrt(count($movies), 1);
/** * Loads a bean from the object database. * It searches for a RedBean_OODBBean Bean Object in the * database. It does not matter how this bean has been stored. * RedBean uses the primary key ID $id and the string $type * to find the bean. The $type specifies what kind of bean you * are looking for; this is the same type as used with the * dispense() function. If RedBean finds the bean it will return * the RedBean_OODB Bean object; if it cannot find the bean * RedBean will return a new bean of type $type and with * primary key ID 0. In the latter case it acts basically the * same as dispense(). * * Important note: * If the bean cannot be found in the database a new bean of * the specified type will be generated and returned. * * @param string $type type of bean you want to load * @param integer $id ID of the bean you want to load * * @throws RedBean_Exception_SQL * * @return RedBean_OODBBean */ public function load($type, $id) { return $this->redbean->load($type, $id); }
public function getParent(RedBean_OODBBean $bean) { return $this->oodb->load($bean->getMeta("type"), (int) $bean->parent_id); }