/** * Finds a bean using a type and a where clause (SQL). * As with most Query tools in RedBean you can provide values to * be inserted in the SQL statement by populating the value * array parameter; you can either use the question mark notation * or the slot-notation (:keyname). * * @param string $type type the type of bean you are looking for * @param string $sql 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 * * @throws Security */ public function find($type, $sql = NULL, $bindings = array()) { if (!is_array($bindings)) { throw new RedException('Expected array, ' . gettype($bindings) . ' given.'); } return $this->redbean->find($type, array(), $sql, $bindings); }
/** * Finds beans by its type and a certain criteria set. * * Format of criteria set: property => value * The criteria set also supports OR-conditions: property => array( value1, orValue2 ) * * If the additional SQL is a condition, this condition will be glued to the rest * of the query using an AND operator. Note that this is as far as this method * can go, there is no way to glue additional SQL using an OR-condition. * This method provides access to an underlying mechanism in the RedBeanPHP architecture * to find beans using criteria sets. However, please do not use this method * for complex queries, use plain SQL instead ( the regular find method ) as it is * more suitable for the job. This method is * meant for basic search-by-example operations. * * @param string $type type of bean to search for * @param array $conditions criteria set describing the bean to search for * @param string $sql additional SQL (for sorting) * * @return array */ public function findLike($type, $conditions = array(), $sql = '') { if (count($conditions) > 0) { foreach ($conditions as $key => $condition) { if (!count($condition)) { unset($conditions[$key]); } } } return $this->redbean->find($type, $conditions, $sql); }
/** * Test the database driver and low level functions. * * @return void */ public function testDriver() { $currentDriver = $this->currentlyActiveDriverID; R::store(R::dispense('justabean')); $adapter = new TroubleDapter(R::getToolBox()->getDatabaseAdapter()->getDatabase()); $adapter->setSQLState('HY000'); $writer = new SQLiteT($adapter); $redbean = new OODB($writer); $toolbox = new ToolBox($redbean, $adapter, $writer); // We can only test this for a known driver... if ($currentDriver === 'sqlite') { try { $redbean->find('bean'); pass(); } catch (\Exception $e) { var_dump($e->getSQLState()); fail(); } } $adapter->setSQLState(-999); try { $redbean->find('bean'); fail(); } catch (\Exception $e) { pass(); } try { $redbean->wipe('justabean'); fail(); } catch (\Exception $e) { pass(); } $toolbox = R::getToolBox(); $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $page = $redbean->dispense("page"); try { $adapter->exec("an invalid query"); fail(); } catch (SQL $e) { pass(); } // Special data type description should result in magic number 99 (specified) if ($currentDriver == 'mysql') { asrt($writer->code(MySQL::C_DATATYPE_SPECIAL_DATE), 99); } if ($currentDriver == 'pgsql') { asrt($writer->code(PostgreSQL::C_DATATYPE_SPECIAL_DATE), 99); } if ($currentDriver == 'CUBRID') { asrt($writer->code(CUBRID::C_DATATYPE_SPECIAL_DATE), 99); } asrt((int) $adapter->getCell("SELECT 123"), 123); $page->aname = "my page"; $id = (int) $redbean->store($page); asrt((int) $page->id, 1); asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 1); asrt($pdo->GetCell("SELECT aname FROM page LIMIT 1"), "my page"); asrt((int) $id, 1); $page = $redbean->load("page", 1); asrt($page->aname, "my page"); asrt((bool) $page->getMeta("type"), TRUE); asrt(isset($page->id), TRUE); asrt($page->getMeta("type"), "page"); asrt((int) $page->id, $id); }