/** * * @param RedBean_OODBBean $parent * @param RedBean_OODBBean $child */ public function attach(RedBean_OODBBean $parent, RedBean_OODBBean $child) { if (!intval($parent->id)) { $this->oodb->store($parent); } $child->{$this->property} = $parent->id; $this->oodb->store($child); }
/** * Attaches the specified child node to the specified parent node. * @param RedBean_OODBBean $parent * @param RedBean_OODBBean $child */ public function attach(RedBean_OODBBean $parent, RedBean_OODBBean $child) { $idfield = $this->writer->getIDField($parent->getMeta("type")); if (!intval($parent->{$idfield})) { $this->oodb->store($parent); } $child->{$this->property} = $parent->{$idfield}; $this->oodb->store($child); }
/** * 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()); }
/** * Swaps a property of two beans. * Given two beans and a property this method swaps the value of the * property in the beans. * * @deprecated * This method does not seem very useful. * * @param array $beans beans * @param string $property property to swap * * @return void */ public function swap($beans, $property) { $bean1 = array_shift($beans); $bean2 = array_shift($beans); $tmp = $bean1->{$property}; $bean1->{$property} = $bean2->{$property}; $bean2->{$property} = $tmp; $this->oodb->store($bean1); $this->oodb->store($bean2); }
/** * Stores a RedBean OODBBean and caches it. * * @param RedBean_OODBBean $bean the bean you want to store * * @return mixed */ public function store($bean) { $id = parent::store($bean); $type = $bean->getMeta('type'); if (!isset($this->cache[$type])) { $this->cache[$type] = array(); } $this->cache[$type][$id] = $bean; return $id; }
/** * Stores a bean and updates cache. * @param RedBean_OODBBean $bean * @return integer $id */ public function store(RedBean_OODBBean $bean) { $this->columnCounter = 0; $type = $bean->getMeta("type"); $idfield = $this->writer->getIDField($type); $newbean = $this->oodb->dispense($type); $newbean->{$idfield} = $bean->{$idfield}; $oldBean = $this->fetchOriginal($bean); //Is there a cached version? if ($oldBean) { //Assume no differences. $dirty = false; //Check for differences. foreach ($oldBean as $p => $v) { if ($v !== $bean->{$p} && $p != $idfield) { $newbean->{$p} = $bean->{$p}; //echo "ADDING CHANGED PROP: $p $v -> ".$bean->$p; $this->columnCounter++; //for tests. //found a difference; mark as tainted. $dirty = true; } } //are there any new props? foreach ($bean as $p => $v) { if (!isset($oldBean->{$p})) { $dirty = true; $newbean->{$p} = $bean->{$p}; $this->columnCounter++; } } //If the bean is dirty; send only differences for update. if ($dirty) { $newbean->copyMetaFrom($bean); $id = $this->oodb->store($newbean); $bean->copyMetaFrom($newbean); $this->putInCache($bean); return $id; } else { return $bean->{$idfield}; } } else { $id = $this->oodb->store($bean); $this->putInCache($bean); return $id; } }
/** * Removes all relations for a bean * @param RedBean_OODBBean $bean * @param string $type */ public function clearRelations(RedBean_OODBBean $bean, $type) { $this->oodb->store($bean); $table = $this->getTable(array($bean->getMeta("type"), $type)); $idfield = $this->writer->getIDField($bean->getMeta("type")); if ($type == $bean->getMeta("type")) { $property2 = $type . "2_id"; $cross = 1; } else { $cross = 0; } $property = $bean->getMeta("type") . "_id"; try { $this->writer->deleteByCrit($table, array($property => $bean->{$idfield})); if ($cross) { $this->writer->deleteByCrit($table, array($property2 => $bean->{$idfield})); } } catch (RedBean_Exception_SQL $e) { if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") { throw $e; } } }
/** * Removes all relations for a bean. This method breaks every connection between * a certain bean $bean and every other bean of type $type. Warning: this method * is really fast because it uses a direct SQL query however it does not inform the * models about this. If you want to notify FUSE models about deletion use a foreach-loop * with unassociate() instead. (that might be slower though) * * @param RedBean_OODBBean $bean reference bean * @param string $type type of beans that need to be unassociated * * @return void */ public function clearRelations(RedBean_OODBBean $bean, $type) { $this->oodb->store($bean); $table = $this->getTable(array($bean->getMeta("type"), $type)); $idfield = $this->writer->getIDField($bean->getMeta("type")); if ($type == $bean->getMeta("type")) { $property2 = $type . "2_id"; $cross = 1; } else { $cross = 0; } $property = $bean->getMeta("type") . "_id"; try { $this->writer->selectRecord($table, array($property => array($bean->{$idfield})), null, true); if ($cross) { $this->writer->selectRecord($table, array($property2 => array($bean->{$idfield})), null, true); } } catch (RedBean_Exception_SQL $e) { if (!$this->writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { throw $e; } } }
/** * Removes all relations for a bean * @param RedBean_OODBBean $bean * @param <type> $type */ public function clearRelations(RedBean_OODBBean $bean, $type) { $this->oodb->store($bean); $table = $this->getTable(array($bean->getMeta("type"), $type)); $idfield = $this->writer->getIDField($bean->getMeta("type")); if ($type == $bean->getMeta("type")) { $property2 = $type . "2_id"; $cross = 1; } else { $cross = 0; } $property = $bean->getMeta("type") . "_id"; $sql = "DELETE FROM `{$table}`\n\t\tWHERE " . $this->adapter->escape($property) . " = " . $this->adapter->escape($bean->{$idfield}); if ($cross) { $sql .= " OR " . $this->adapter->escape($property2) . " = " . $this->adapter->escape($bean->{$idfield}); } try { $this->adapter->exec($sql); } catch (RedBean_Exception_SQL $e) { if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") { throw $e; } } }
/** * Stores a RedBean OODB Bean and returns the ID. * * @param RedBean_OODBBean|RedBean_SimpleModel $bean bean * * @return integer $id id */ public static function store($bean) { return self::$redbean->store($bean); }
/** * Saves the current domain object. * The function saves the inner bean to the database. */ public function save() { $this->redbean->store($this->bean); }
$writer = $oldwriter; $redbean = $oldredbean; testpack("Test Custom ID Field"); class MyWriter extends RedBean_QueryWriter_PostgreSQL { public function getIDField($type) { return $type . "_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");
/** * Stores a bean in the database. This method takes a * RedBean_OODBBean Bean Object $bean and stores it * in the database. If the database schema is not compatible * with this bean and RedBean runs in fluid mode the schema * will be altered to store the bean correctly. * If the database schema is not compatible with this bean and * RedBean runs in frozen mode it will throw an exception. * This function returns the primary key ID of the inserted * bean. * * The return value is an integer if possible. If it is not possible to * represent the value as an integer a string will be returned. * * @param RedBean_OODBBean|RedBean_SimpleModel $bean bean to store * * @return integer|string * * @throws RedBean_Exception_Security */ public function store($bean) { return $this->redbean->store($bean); }
/** * Stores a RedBean OODB Bean and returns the ID. * @param RedBean_OODBBean $bean * @return integer $id */ public static function store(RedBean_OODBBean $bean) { return self::$redbean->store($bean); }