/** * Fetches a collection of OODB Bean objects based on the SQL * criteria provided. For instance; * * - Finder::where("page", " name LIKE '%more%' "); * * Will return all pages that have the word 'more' in their name. * The second argument is actually just plain SQL; the function expects * this SQL to be compatible with a SELECT * FROM TABLE WHERE X query, * where X is ths search string you provide in the second parameter. * Another example, using slots: * * - Finder::where("page", " name LIKE :str ",array(":str"=>'%more%')); * * Also, note that the default search is always 1. So if you do not * specify a search parameter this function will just return every * bean of the given type: * * - Finder::where("page"); //returns all pages * * * @param string $type * @param string $SQL * @param array $values * @return array $beans */ public static function where($type, $SQL = " 1 ", $values = array()) { //Sorry, quite draconic filtering $type = preg_replace("/\\W/", "", $type); //First get hold of the toolbox $tools = RedBean_Setup::getToolBox(); RedBean_CompatManager::scanDirect($tools, array(RedBean_CompatManager::C_SYSTEM_MYSQL => "5")); //Now get the two tools we need; RedBean and the Adapter $redbean = $tools->getRedBean(); $adapter = $tools->getDatabaseAdapter(); //Make a standard ANSI SQL query from the SQL provided try { $SQL = "SELECT * FROM {$type} WHERE " . $SQL; //Fetch the values using the SQL and value pairs provided $rows = $adapter->get($SQL, $values); } catch (RedBean_Exception_SQL $e) { if ($e->getSQLState() == "42S02" || $e->getSQLState() == "42S22") { //no such table? no problem. may happen. return array(); } else { throw $e; } } //Give the rows to RedBean OODB to convert them //into beans. return $redbean->convertToBeans($type, $rows); }
/** * * Constructor, requires a type name * @param string $typeName */ public function __construct($typeName) { $this->tools = RedBean_Setup::getToolBox(); $this->redbean = $this->tools->getRedBean(); $this->bean = $this->redbean->dispense($typeName); $this->associationManager = new RedBean_AssociationManager($this->tools); $this->treeManager = new RedBean_TreeManager($this->tools); }
/** * Ensures that given an association between * $bean1 and $bean2, * if one of them gets trashed the association will be * automatically removed. * @param RedBean_OODBBean $bean1 * @param RedBean_OODBBean $bean2 * @return boolean $addedFKS */ public static function addConstraint(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $dontCache = false) { //Fetch the toolbox $toolbox = RedBean_Setup::getToolBox(); RedBean_CompatManager::scanDirect($toolbox, array(RedBean_CompatManager::C_SYSTEM_MYSQL => "5")); //Create an association manager $association = new RedBean_AssociationManager($toolbox); $writer = $toolbox->getWriter(); $oodb = $toolbox->getRedBean(); $adapter = $toolbox->getDatabaseAdapter(); //Frozen? Then we may not alter the schema! if ($oodb->isFrozen()) { return false; } //$adapter->getDatabase()->setDebugMode(1); $table1 = $bean1->getMeta("type"); $table2 = $bean2->getMeta("type"); $table = $association->getTable(array($table1, $table2)); $idfield1 = $writer->getIDField($bean1->getMeta("type")); $idfield2 = $writer->getIDField($bean2->getMeta("type")); $bean = $oodb->dispense($table); $property1 = $bean1->getMeta("type") . "_id"; $property2 = $bean2->getMeta("type") . "_id"; if ($property1 == $property2) { $property2 = $bean2->getMeta("type") . "2_id"; } $table = $adapter->escape($table); $table1 = $adapter->escape($table1); $table2 = $adapter->escape($table2); $property1 = $adapter->escape($property1); $property2 = $adapter->escape($property2); //In Cache? Then we dont need to bother if (isset(self::$fkcache[$table])) { return false; } $db = $adapter->getCell("select database()"); $fks = $adapter->getCell("\n\t\t\tSELECT count(*)\n\t\t\tFROM information_schema.KEY_COLUMN_USAGE\n\t\t\tWHERE TABLE_SCHEMA ='{$db}' AND TABLE_NAME ='{$table}' AND\n\t\t\tCONSTRAINT_NAME <>'PRIMARY' AND REFERENCED_TABLE_NAME is not null\n\t\t"); //already foreign keys added in this association table if ($fks > 0) { return false; } //add the table to the cache, so we dont have to fire the fk query all the time. if (!$dontCache) { self::$fkcache[$table] = true; } $columns = $writer->getColumns($table); if ($writer->code($columns[$property1]) !== RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32) { $writer->widenColumn($table, $property1, RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32); } if ($writer->code($columns[$property2]) !== RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32) { $writer->widenColumn($table, $property2, RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32); } $sql = "\n\t\t\tALTER TABLE " . $writer->noKW($table) . "\n\t\t\tADD FOREIGN KEY({$property1}) references {$table1}(id) ON DELETE CASCADE;\n\n\t\t"; $adapter->exec($sql); $sql = "\n\t\t\tALTER TABLE " . $writer->noKW($table) . "\n\t\t\tADD FOREIGN KEY({$property2}) references {$table2}(id) ON DELETE CASCADE\n\t\t"; $adapter->exec($sql); return true; }
/** * Fetches a collection of OODB Bean objects based on the SQL * criteria provided. For instance; * * - Finder::where("page", " name LIKE '%more%' "); * * Will return all pages that have the word 'more' in their name. * The second argument is actually just plain SQL; the function expects * this SQL to be compatible with a SELECT * FROM TABLE WHERE X query, * where X is ths search string you provide in the second parameter. * Another example, using slots: * * - Finder::where("page", " name LIKE :str ",array(":str"=>'%more%')); * * Also, note that the default search is always 1. So if you do not * specify a search parameter this function will just return every * bean of the given type: * * - Finder::where("page"); //returns all pages * * * @param string $type * @param string $SQL * @param array $values * @return array $beans */ public static function where($type, $SQL = " 1 ", $values = array()) { //Sorry, quite draconic filtering $type = preg_replace("/\\W/", "", $type); //First get hold of the toolbox $tools = RedBean_Setup::getToolBox(); //Now get the two tools we need; RedBean and the Adapter $redbean = $tools->getRedBean(); $adapter = $tools->getDatabaseAdapter(); //Make a standard ANSI SQL query from the SQL provided $SQL = "SELECT * FROM {$type} WHERE " . $SQL; //Fetch the values using the SQL and value pairs provided $rows = $adapter->get($SQL, $values); //Give the rows to RedBean OODB to convert them //into beans. return $redbean->convertToBeans($type, $rows); }
/** * Returns a collection of Domain Objects. * @param string $type * @param string $query * @return array $collection */ public static function getDomainObjects($type, $query = "1", $values = array()) { //Fetch us a toolbox. $tools = RedBean_Setup::getToolBox(); $redbean = $tools->getRedBean(); $domainObject = new $type(); $typeName = $domainObject->bean->getMeta("type"); $collection = array(); $finder = new \RedBean_Plugin_Finder(); $beans = $finder->where($typeName, $query, $values); foreach ($beans as $bean) { $domainObject = new $type(); $domainObject->bean = $bean; $collection[] = $domainObject; } return $collection; }
/** * 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); }
/** * 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 ); $toolbox = RedBean_Setup::getToolBox(); self::configureFacadeWithToolbox($toolbox); }
/** * Fetches a collection of OODB Bean objects based on the SQL * criteria provided. For instance; * * - Finder::where("page", " name LIKE '%more%' "); * * Will return all pages that have the word 'more' in their name. * The second argument is actually just plain SQL; the function expects * this SQL to be compatible with a SELECT * FROM TABLE WHERE X query, * where X is ths search string you provide in the second parameter. * Another example, using slots: * * - Finder::where("page", " name LIKE :str ",array(":str"=>'%more%')); * * Also, note that the default search is always 1. So if you do not * specify a search parameter this function will just return every * bean of the given type: * * - Finder::where("page"); //returns all pages * * * @param string $type * @param string $SQL * @param array $values * @return array $beans */ public static function where($type, $SQL = " 1 ", $values = array(), $tools = false, $ignoreGSQLWarn = false) { if ($SQL === "") { $SQL = " 1 "; } //Sorry, quite draconic filtering $type = preg_replace("/\\W/", "", $type); //First get hold of the toolbox if (!$tools) { $tools = RedBean_Setup::getToolBox(); } RedBean_CompatManager::scanDirect($tools, array(RedBean_CompatManager::C_SYSTEM_MYSQL => "5", RedBean_CompatManager::C_SYSTEM_SQLITE => "3", RedBean_CompatManager::C_SYSTEM_POSTGRESQL => "7")); //Now get the two tools we need; RedBean and the Adapter $redbean = $tools->getRedBean(); $adapter = $tools->getDatabaseAdapter(); $writer = $tools->getWriter(); //Do we need to parse Gold SQL? if (!$redbean->isFrozen()) { $SQL = self::parseGoldSQL($SQL, $type, $tools); } else { if (!$ignoreGSQLWarn && strpos($SQL, "@") !== false) { throw new RedBean_Exception_SQL("Gold SQL is\n\t\t\t\t\tonly allowed in FLUID mode,\n\t\t\t\t\tto ignore use extra argument TRUE for Finder::Where"); } } //Make a standard ANSI SQL query from the SQL provided try { $SQL = "SELECT * FROM {$type} WHERE " . $SQL; //Fetch the values using the SQL and value pairs provided $rows = $adapter->get($SQL, $values); } catch (RedBean_Exception_SQL $e) { if ($writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return array(); } else { throw $e; } } //Give the rows to RedBean OODB to convert them //into beans. return $redbean->convertToBeans($type, $rows); }