/** * Tests the various ways to fetch (select queries) * data using adapter methods in the facade. * Also tests the new R::getAssocRow() method, * as requested in issue #324. */ public function testFetchTypes() { R::nuke(); $page = R::dispense('page'); $page->a = 'a'; $page->b = 'b'; R::store($page); $page = R::dispense('page'); $page->a = 'c'; $page->b = 'd'; R::store($page); $expect = '[{"id":"1","a":"a","b":"b"},{"id":"2","a":"c","b":"d"}]'; asrt(json_encode(R::getAll('SELECT * FROM page')), $expect); $expect = '{"1":"a","2":"c"}'; asrt(json_encode(R::getAssoc('SELECT id, a FROM page')), $expect); asrt(json_encode(R::getAssoc('SELECT id, a, b FROM page')), $expect); $expect = '[{"id":"1","a":"a"},{"id":"2","a":"c"}]'; asrt(json_encode(R::getAssocRow('SELECT id, a FROM page')), $expect); $expect = '[{"id":"1","a":"a","b":"b"},{"id":"2","a":"c","b":"d"}]'; asrt(json_encode(R::getAssocRow('SELECT id, a, b FROM page')), $expect); $expect = '{"id":"1","a":"a","b":"b"}'; asrt(json_encode(R::getRow('SELECT * FROM page WHERE id = 1')), $expect); $expect = '"a"'; asrt(json_encode(R::getCell('SELECT a FROM page WHERE id = 1')), $expect); $expect = '"b"'; asrt(json_encode(R::getCell('SELECT b FROM page WHERE id = 1')), $expect); $expect = '"c"'; asrt(json_encode(R::getCell('SELECT a FROM page WHERE id = 2')), $expect); $expect = '["a","c"]'; asrt(json_encode(R::getCol('SELECT a FROM page')), $expect); $expect = '["b","d"]'; asrt(json_encode(R::getCol('SELECT b FROM page')), $expect); }
/** * Tests whether we can send results of a query to meta data * when converting to bean. */ public function testImportMeta() { R::nuke(); $book = R::dispense(array('_type' => 'book', 'title' => 'Bean Recipes', 'author' => 'Meastro de la Bean')); $pages = R::dispenseAll('page*2'); $book->ownPageList = reset($pages); R::store($book); $data = R::getRow('SELECT book.*, COUNT(page.id) AS meta_count, 1234 AS meta_extra FROM book LEFT JOIN page ON page.book_id = book.id GROUP BY book.id '); $bean = R::convertToBean('book', $data, 'meta_'); asrt(isset($bean->title), TRUE); asrt(isset($bean->author), TRUE); asrt(isset($bean->meta_count), FALSE); asrt(isset($bean->meta_extra), FALSE); $data = $bean->getMeta('data.bundle'); asrt(intval($data['meta_count']), 2); asrt(intval($data['meta_extra']), 1234); //now with multiple beans $book = R::dispense(array('_type' => 'book', 'title' => 'Bean Adventures', 'author' => 'Mr Adventure')); $pages = R::dispenseAll('page*3'); $book->ownPageList = reset($pages); R::store($book); $data = R::getAll('SELECT book.*, COUNT(page.id) AS meta_pages FROM book LEFT JOIN page ON page.book_id = book.id GROUP BY book.id '); $books = R::convertToBeans('book', $data, 'meta_'); $found = 0; foreach ($books as $book) { if ($book->title == 'Bean Recipes') { $found++; asrt(isset($book->title), TRUE); asrt(isset($book->author), TRUE); asrt(isset($book->meta_count), FALSE); asrt(isset($book->meta_extra), FALSE); $data = $book->getMeta('data.bundle'); asrt(intval($data['meta_pages']), 2); } if ($book->title == 'Bean Adventures') { $found++; asrt(isset($book->title), TRUE); asrt(isset($book->author), TRUE); asrt(isset($book->meta_pages), FALSE); asrt(isset($book->meta_extra), FALSE); $data = $book->getMeta('data.bundle'); asrt(intval($data['meta_pages']), 3); } } asrt($found, 2); }
/** * @param string $user * @param string $pass */ public function check($user, $pass) { $this->rememberMe = isset($_POST['rememberme']) ? true : false; if (isset($user) && !empty($user) && isset($pass) && !empty($pass)) { $result = Facade::getRow("SELECT * FROM users WHERE\r\n user_name = :user AND user_pass = :pass", [':user' => $user, ':pass' => $this->factory->getUsersObj()->getHash($user)]); if (!empty($result) && password_verify($pass, $result['user_pass'])) { $this->userName = $result['user_name']; $this->userID = $result['id']; $this->email = $result['email']; $this->authLogin(); if ($this->rememberMe) { $this->setRememberme($this->userID); } echo General::Ref($this->logPage); } else { echo General::messageSent('Wrong Username or Password', $this->indexPage); } } }
public function clearlogbydate($startDate, $endDate, $userid) { try { $buser = (object) R::getRow('select * from users where user_name = ? ', array($userid)); $sql = "DELETE FROM `systemlog` WHERE DATE_FORMAT(create_date,'%Y-%m-%d') BETWEEN '{$startDate}' and '{$endDate}'"; $item = new Systemlog(); $item->query = $sql; $item->userid = $userid; $item->tbname = $this->tbname; if ($buser->groups_id == 1 && $buser->user_level > 5) { $rows = R::exec($sql); $item->parametor = "{$startDate}/{$endDate}/numrows = " . $rows; $item->logs = 'Clear Log between ' . $startDate . ' to ' . $endDate; $item->types = 'CLEAR LOGS'; $this->insertlog($item, $userid); } else { $item->parametor = "{$startDate}/{$endDate}"; $item->logs = 'Warning::Clear Log between ' . $startDate . ' to ' . $endDate; $item->types = 'Warning::CLEAR LOGS'; $this->insertlog($item, $userid); throw new Exception('คุณไม่มีสิทธิ์ ในการ ลย'); } } catch (Exception $e) { throw new Exception($e->getMessage()); } }
/** * Begin testing. * This method runs the actual test pack. * * @return void */ public function testFinding() { $toolbox = R::getToolBox(); $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $a = new AssociationManager($toolbox); $page = $redbean->dispense("page"); $page->name = "John's page"; $idpage = $redbean->store($page); $page2 = $redbean->dispense("page"); $page2->name = "John's second page"; $idpage2 = $redbean->store($page2); $a->associate($page, $page2); $pageOne = $redbean->dispense("page"); $pageOne->name = "one"; $pageMore = $redbean->dispense("page"); $pageMore->name = "more"; $pageEvenMore = $redbean->dispense("page"); $pageEvenMore->name = "evenmore"; $pageOther = $redbean->dispense("page"); $pageOther->name = "othermore"; set1toNAssoc($a, $pageOther, $pageMore); set1toNAssoc($a, $pageOne, $pageMore); set1toNAssoc($a, $pageOne, $pageEvenMore); asrt(count($redbean->find("page", array(), " name LIKE '%more%' ", array())), 3); asrt(count($redbean->find("page", array(), " name LIKE :str ", array(":str" => '%more%'))), 3); asrt(count($redbean->find("page", array(), array(" name LIKE :str ", array(":str" => '%more%')))), 3); asrt(count($redbean->find("page", array(), " name LIKE :str ", array(":str" => '%mxore%'))), 0); asrt(count($redbean->find("page", array("id" => array(2, 3)))), 2); $bean = $redbean->dispense("wine"); $bean->name = "bla"; for ($i = 0; $i < 10; $i++) { $redbean->store($bean); } $redbean->find("wine", array("id" => 5)); // Finder:where call OODB::convertToBeans $bean2 = $redbean->load("anotherbean", 5); asrt($bean2->id, 0); $keys = $adapter->getCol("SELECT id FROM page WHERE " . $writer->esc('name') . " LIKE '%John%'"); asrt(count($keys), 2); $pages = $redbean->batch("page", $keys); asrt(count($pages), 2); $p = R::findLast('page'); pass(); $row = R::getRow('select * from page '); asrt(is_array($row), TRUE); asrt(isset($row['name']), TRUE); // Test findAll -- should not throw an exception asrt(count(R::findAll('page')) > 0, TRUE); asrt(count(R::findAll('page', ' ORDER BY id ')) > 0, TRUE); $beans = R::findOrDispense("page"); asrt(count($beans), 6); asrt(is_null(R::findLast('nothing')), TRUE); try { R::find('bean', ' id > 0 ', 'invalid bindings argument'); fail(); } catch (RedException $exception) { pass(); } }
public static function updateRecord($tbname, $item, $type = null, $userid = 'system') { try { $item = self::ConvertoObj($item, $type); $sql = 'select * from ' . $tbname . ' where ' . $item->getKey() . ' = ' . $item->getId(); // echo $sql; $row = R::getRow($sql); $rsItem = self::importArraytoAttay($row, $item); $sql = 'update ' . $tbname . ' set '; $length = sizeof($rsItem); $i = 0; foreach ($rsItem as $key => $val) { $sql .= $key . '= \'' . $val . '\''; $i++; if ($i < $length) { $sql .= ' , '; } } $sql .= ' WHERE ' . $item->getKey() . '=' . $item->getId(); self::$stmt = self::$connection->prepare($sql); if (self::$stmt->execute()) { return self::$item->getId(); } else { throw new Exception('Con\'t Insert'); } } catch (exception $e) { throw new Exception($e->getMessage()); } }