/** * Returns the bean identified by the RESTful path. * For instance: * * $user * /site/1/page/3 * * returns page with ID 3 in ownPage of site 1 in ownSite of * $user bean. * * Works with shared lists as well: * * $user * /site/1/page/3/shared-ad/4 * * Note that this method will open all intermediate beans so you can * attach access control rules to each bean in the path. * * @param RedBean_OODBBean $bean * @param array $steps (an array representation of a REST path) * * @return RedBean_OODBBean * * @throws RedBean_Exception_Security */ public function findByPath($bean, $steps) { $numberOfSteps = count($steps); if (!$numberOfSteps) { return $bean; } if ($numberOfSteps % 2) { throw new RedBean_Exception_Security('Invalid path: needs 1 more element.'); } for ($i = 0; $i < $numberOfSteps; $i += 2) { $steps[$i] = trim($steps[$i]); if ($steps[$i] === '') { throw new RedBean_Exception_Security('Cannot access list.'); } if (strpos($steps[$i], 'shared-') === FALSE) { $listName = 'own' . ucfirst($steps[$i]); $listType = $this->toolbox->getWriter()->esc($steps[$i]); } else { $listName = 'shared' . ucfirst(substr($steps[$i], 7)); $listType = $this->toolbox->getWriter()->esc(substr($steps[$i], 7)); } $list = $bean->withCondition(" {$listType}.id = ? ", array($steps[$i + 1]))->{$listName}; if (!isset($list[$steps[$i + 1]])) { throw new RedBean_Exception_Security('Cannot access bean.'); } $bean = $list[$steps[$i + 1]]; } return $bean; }
/** * Constructor * * @param RedBean_ToolBox $tools toolbox */ public function __construct(RedBean_ToolBox $tools) { $this->oodb = $tools->getRedBean(); $this->adapter = $tools->getDatabaseAdapter(); $this->writer = $tools->getWriter(); $this->toolbox = $tools; }
/** * Properly untether new clone by re-configuring with a fresh toolbox * * @return void */ public function __clone() { $class = get_class($this->toolbox->getWriter()); $writer = new $class($this->toolbox->getDatabaseAdapter()); $redbean = new RedBean_OODB($writer); $toolbox = new RedBean_ToolBox($redbean, $this->toolbox->getDatabaseAdapter(), $writer); $this->configureWithToolbox($toolbox); }
/** * Constructor, requires a writer * @param RedBean_QueryWriter $writer */ public function __construct(RedBean_ToolBox $toolbox) { $this->writer = $toolbox->getWriter(); $this->adapter = $toolbox->getDatabaseAdapter(); $this->redbean = $toolbox->getRedBean(); if ($this->redbean->isFrozen()) { $this->adapter->exec("\n\t\t\t\t\t\tCREATE TABLE IF NOT EXISTS `__log` (\n\t\t\t\t\t\t`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,\n\t\t\t\t\t\t`tbl` VARCHAR( 255 ) NOT NULL ,\n\t\t\t\t\t\t`action` TINYINT( 2 ) NOT NULL ,\n\t\t\t\t\t\t`itemid` INT( 11 ) NOT NULL\n\t\t\t\t\t\t) ENGINE = MYISAM ;\n\t\t\t\t"); //Must be MyISAM! else you run in trouble if you use transactions! } $maxid = $this->adapter->getCell("SELECT MAX(id) FROM __log"); $this->adapter->exec("DELETE FROM __log WHERE id < {$maxid} - 200 "); }
/** * Configures the facade, want to have a new Writer? A new Object Database or a new * Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new * toolbox. * * @param RedBean_ToolBox $tb toolbox * * @return RedBean_ToolBox */ public function configureFacadeWithToolbox(RedBean_ToolBox $tb) { $oldTools = $this->toolbox; $this->toolbox = $tb; $this->writer = $this->toolbox->getWriter(); $this->adapter = $this->toolbox->getDatabaseAdapter(); $this->redbean = $this->toolbox->getRedBean(); $this->finder = new RedBean_Finder($this->toolbox); $this->associationManager = new RedBean_AssociationManager($this->toolbox); $this->redbean->setAssociationManager($this->associationManager); $this->labelMaker = new RedBean_LabelMaker($this->toolbox); $this->extAssocManager = new RedBean_AssociationManager_ExtAssociationManager($this->toolbox); $helper = new RedBean_ModelHelper(); $helper->attachEventListeners($this->redbean); $this->associationManager->addEventListener('delete', $helper); $this->duplicationManager = new RedBean_DuplicationManager($this->toolbox); $this->tagManager = new RedBean_TagManager($this->toolbox); $this->f = new RedBean_SQLHelper($this->adapter); return $oldTools; }
/** * Configures the facade, want to have a new Writer? A new Object Database or a new * Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new * toolbox. * * @param RedBean_ToolBox $tb toolbox * * @return RedBean_ToolBox */ public static function configureFacadeWithToolbox(RedBean_ToolBox $tb) { $oldTools = self::$toolbox; self::$toolbox = $tb; self::$writer = self::$toolbox->getWriter(); self::$adapter = self::$toolbox->getDatabaseAdapter(); self::$redbean = self::$toolbox->getRedBean(); self::$finder = new RedBean_Finder(self::$toolbox); self::$associationManager = new RedBean_AssociationManager(self::$toolbox); self::$redbean->setAssociationManager(self::$associationManager); self::$labelMaker = new RedBean_LabelMaker(self::$toolbox); self::$extAssocManager = new RedBean_AssociationManager_ExtAssociationManager(self::$toolbox); $helper = new RedBean_ModelHelper(); $helper->attachEventListeners(self::$redbean); self::$associationManager->addEventListener('delete', $helper); self::$duplicationManager = new RedBean_DuplicationManager(self::$toolbox); self::$tagManager = new RedBean_TagManager(self::$toolbox); self::$f = new RedBean_SQLHelper(self::$adapter); return $oldTools; }
/** * Configures the facade, want to have a new Writer? A new Object Database or a new * Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new * toolbox. * * @static * @param RedBean_ToolBox $tb toolbox * * @return RedBean_ToolBox $tb old, rusty, previously used toolbox */ public static function configureFacadeWithToolbox(RedBean_ToolBox $tb) { $oldTools = self::$toolbox; self::$toolbox = $tb; self::$writer = self::$toolbox->getWriter(); self::$adapter = self::$toolbox->getDatabaseAdapter(); self::$redbean = self::$toolbox->getRedBean(); self::$associationManager = new RedBean_AssociationManager(self::$toolbox); self::$redbean->setAssociationManager(self::$associationManager); self::$extAssocManager = new RedBean_ExtAssociationManager(self::$toolbox); $helper = new RedBean_ModelHelper(); self::$redbean->addEventListener("update", $helper); self::$redbean->addEventListener("open", $helper); self::$redbean->addEventListener("delete", $helper); self::$associationManager->addEventListener("delete", $helper); self::$redbean->addEventListener("after_delete", $helper); self::$redbean->addEventListener("after_update", $helper); self::$redbean->addEventListener("dispense", $helper); return $oldTools; }
/** * Returns the ID of the Model. */ public function getID() { $idField = $this->tools->getWriter()->getIDField($this->bean->getMeta("type")); return $this->bean->{$idField}; }
/** * Constructor. * This Object requires a toolbox. * * @param RedBean_ToolBox $toolbox toolbox for DB operations. */ public function __construct(RedBean_ToolBox $toolbox) { $this->writer = $toolbox->getWriter(); $this->adapter = $toolbox->getDatabaseAdapter(); }
public static function load($post, RedBean_ToolBox $toolbox) { $writer = $toolbox->getWriter(); if (isset($post["associations"])) { $associations = $post["associations"]; unset($post["associations"]); } $can = $pairs = $sorted = array(); foreach ($post as $key => $rawBean) { if (is_array($rawBean) && isset($rawBean["type"])) { $type = $rawBean["type"]; unset($rawBean["type"]); $idfield = $writer->getIDField($type); if (isset($rawBean[$idfield])) { $id = $rawBean[$idfield]; if ($id == 0 && count($rawBean) === 1) { continue; } unset($rawBean[$idfield]); $bean = R::load($type, $id); } else { $bean = R::dispense($type); } foreach ($rawBean as $field => $value) { if (!empty($value)) { $bean->{$field} = $value; } } $can[$key] = $bean; if (!isset($sorted[$type])) { $sorted[$type] = array(); } $sorted[$type][] = $bean; } } if (isset($associations) && is_array($associations)) { foreach ($associations as $assoc) { foreach ($assoc as $info) { if ($info == "0" || $info == "") { continue; } $keys = explode("-", $info); if (isset($can[$keys[0]])) { $bean1 = $can[$keys[0]]; } else { $loader = explode(":", $keys[0]); $bean1 = R::load($loader[0], $loader[1]); } $bean2 = $can[$keys[1]]; $pairs[] = array($bean1, $bean2); } } } return array("can" => $can, "pairs" => $pairs, "sorted" => $sorted); }
/** * Kickstarts redbean for you. * @param string $dsn * @param string $username * @param string $password */ public static function setup($dsn = "sqlite:/tmp/red.db", $username = NULL, $password = NULL) { RedBean_Setup::kickstart($dsn, $username, $password); self::$toolbox = RedBean_Setup::getToolBox(); self::$writer = self::$toolbox->getWriter(); self::$adapter = self::$toolbox->getDatabaseAdapter(); self::$redbean = self::$toolbox->getRedBean(); self::$associationManager = new RedBean_AssociationManager(self::$toolbox); self::$treeManager = new RedBean_TreeManager(self::$toolbox); self::$linkManager = new RedBean_LinkManager(self::$toolbox); self::$extAssocManager = new RedBean_ExtAssociationManager(self::$toolbox); $helper = new RedBean_ModelHelper(); self::$redbean->addEventListener("update", $helper); self::$redbean->addEventListener("open", $helper); self::$redbean->addEventListener("delete", $helper); }
/** * Constructor. * @param RedBean_OODB $oodb * @param RedBean_ToolBox $toolBox */ public function __construct(RedBean_OODB $oodb, RedBean_ToolBox $toolBox) { $this->oodb = $oodb; $this->writer = $toolBox->getWriter(); }
/** * This method will inspect the array provided and load/dispense the * desired beans. To dispense a new bean, the array must contain: * * array( "newuser"=> array("type"=>"user","name"=>"John") ) * * - Creates a new bean of type user, property name is set to "John" * * To load a bean (for association): * * array( "theaddress"=> array("type"=>"address","id"=>2) ) * * - Loads a bean of type address with ID 2 * * Now to associate this bean in your form: * * array("associations"=>array( "0" => array( "newuser-theaddress" ) )) * * - Associates the beans under keys newuser and theaddress. * * To modify an existing bean: * * array("existinguser"=>array("type"=>"user","id"=>2,"name"=>"Peter")) * * - Changes name of bean of type user with ID 2 to 'Peter' * * This function returns: * * array( * "can" => an array with beans, either loaded or dispensed and populated * "pairs" => an array with pairs of beans to be associated * "sorted" => sorted by type * ); * * Note that this function actually does not store or associate anything at all, * it just prepares two arrays. * * @static * @param $post the POST array containing the form data * @return array hash table containing 'can' and 'pairs' * */ public static function load($post, RedBean_ToolBox $toolbox) { $writer = $toolbox->getWriter(); //fetch associations first and remove them from the array. if (isset($post["associations"])) { $associations = $post["associations"]; unset($post["associations"]); } //We store beans here $can = $pairs = $sorted = array(); foreach ($post as $key => $rawBean) { if (is_array($rawBean) && isset($rawBean["type"])) { //get type and remove it from array $type = $rawBean["type"]; unset($rawBean["type"]); //does it have an ID? $idfield = $writer->getIDField($type); if (isset($rawBean[$idfield])) { //yupz, get the id and remove it from array $id = $rawBean[$idfield]; //ID == 0, and no more fields then this is an NULL option for a relation, skip. if ($id == 0 && count($rawBean) === 1) { continue; } unset($rawBean[$idfield]); //now we have the id, load the bean and store it in the can $bean = RedBean_Facade::load($type, $id); } else { //no id? then get a new bean... $bean = RedBean_Facade::dispense($type); } //do we need to modify this bean? foreach ($rawBean as $field => $value) { if (!empty($value)) { $bean->{$field} = $value; } elseif (!self::$dontSetEmptyValues) { $bean->{$field} = $value; } } $can[$key] = $bean; if (!isset($sorted[$type])) { $sorted[$type] = array(); } $sorted[$type][] = $bean; } } if (isset($associations) && is_array($associations)) { foreach ($associations as $assoc) { foreach ($assoc as $info) { if ($info == "0" || $info == "") { continue; } $keys = explode("-", $info); //first check if we can find the key in the can, --only key 1 is able to load if (isset($can[$keys[0]])) { $bean1 = $can[$keys[0]]; } else { $loader = explode(":", $keys[0]); $bean1 = RedBean_Facade::load($loader[0], $loader[1]); } $bean2 = $can[$keys[1]]; $pairs[] = array($bean1, $bean2); } } } return array("can" => $can, "pairs" => $pairs, "sorted" => $sorted); }