find() public static method

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).
public static find ( string $type, string $sql = NULL, array $bindings = [] ) : array
$type string the type of bean you are looking for
$sql string SQL query to find the desired bean, starting right after WHERE clause
$bindings array array of values to be bound to parameters in query
return array
 /**
  * Logs admin into the system
  * @param $login
  * @param $password
  * @return \SkullyAdmin\Models\Admin|null
  */
 public function login($login, $password)
 {
     /** @var \RedBean_SimpleModel $adminBean */
     $adminBean = R::findOne('admin', "status = ? and email = ?", array(Admin::STATUS_ACTIVE, $login));
     if (!empty($adminBean)) {
         /** @var \SkullyAdmin\Models\Admin $admin */
         $admin = $adminBean->box();
         if ($admin->get('password_hash') == UtilitiesHelper::toHash($password, $admin->get('salt'), $this->app->config('globalSalt'))) {
             $adminSessions = R::find('adminsession', "admin_id = ?", array($admin->getID()));
             if (!empty($adminSessions)) {
                 R::trashAll($adminSessions);
             }
             // when everything ok, regenerate session
             session_regenerate_id(true);
             // change session ID for the current session and invalidate old session ID
             $adminId = $admin->getID();
             $sessionId = session_id();
             $adminsession = $this->app->createModel('adminsession', array("admin_id" => $adminId, "session_id" => $sessionId));
             $this->app->getSession()->set('adminId', $admin->getID());
             R::store($adminsession);
             return $admin;
         }
     }
     return null;
 }
Beispiel #2
0
 /**
  * Test to make sure stash cache works with recursively opening models
  * with FUSE.
  *
  * @return void
  */
 public function testIssue259()
 {
     testpack('Testing Issue #259 - Stash Cache breaks model delegation in open().');
     $mother = R::dispense('mother');
     $mother->desc = 'I am mother';
     R::store($mother);
     $child = R::dispense('child');
     $child->mother = $mother;
     $child->desc = 'I am child';
     $id = R::store($child);
     R::findOne('child', ' id = ?', array($id));
     R::find('child', ' id = ? ', array($id));
     R::load('child', $id);
 }
Beispiel #3
0
 /**
  * In the past it was not possible to export beans
  * like 'feed' (Model_Feed).
  *
  * @return void
  */
 public function testExportIssue()
 {
     R::nuke();
     $feed = R::dispense('feed');
     $feed->post = array('first', 'second');
     R::store($feed);
     $rows = R::getAll('SELECT * FROM feed');
     asrt($rows[0]['post'], '["first","second"]');
     $feed = $feed->fresh();
     asrt(is_array($feed->post), TRUE);
     asrt($feed->post[0], 'first');
     asrt($feed->post[1], 'second');
     R::store($feed);
     $rows = R::getAll('SELECT * FROM feed');
     asrt($rows[0]['post'], '["first","second"]');
     $feed = R::load('feed', $feed->id);
     $feed->post[] = 'third';
     R::store($feed);
     $rows = R::getAll('SELECT * FROM feed');
     asrt($rows[0]['post'], '["first","second","third"]');
     $feed = $feed->fresh();
     asrt(is_array($feed->post), TRUE);
     asrt($feed->post[0], 'first');
     asrt($feed->post[1], 'second');
     asrt($feed->post[2], 'third');
     //now the catch: can we use export?
     //PHP Fatal error:  Call to a member function export() on a non-object
     $feeds = R::exportAll(R::find('feed'));
     asrt(is_array($feeds), TRUE);
     $feed = reset($feeds);
     asrt($feed['post'][0], 'first');
     asrt($feed['post'][1], 'second');
     asrt($feed['post'][2], 'third');
     //can we also dup()?
     $feedOne = R::findOne('feed');
     R::store(R::dup($feedOne));
     asrt(R::count('feed'), 2);
     //can we delete?
     R::trash($feedOne);
     asrt(R::count('feed'), 1);
     $feedTwo = R::findOne('feed');
     $feed = $feedTwo->export();
     asrt($feed['post'][0], 'first');
     asrt($feed['post'][1], 'second');
     asrt($feed['post'][2], 'third');
 }
Beispiel #4
0
 /**
  * Test effects of cache.
  * 
  * @return void
  */
 public function testCachingEffects()
 {
     testpack('Testing WriteCache Query Writer Cache');
     R::useWriterCache(FALSE);
     R::debug(true, 1);
     $logger = R::getDatabaseAdapter()->getDatabase()->getLogger();
     $book = R::dispense('book')->setAttr('title', 'ABC');
     $book->ownPage[] = R::dispense('page');
     $id = R::store($book);
     // Test load cache -- without
     $logger->clear();
     $book = R::load('book', $id);
     $book = R::load('book', $id);
     asrt(count($logger->grep('SELECT')), 2);
     // With cache
     R::useWriterCache(TRUE);
     $logger->clear();
     $book = R::load('book', $id);
     $book = R::load('book', $id);
     asrt(count($logger->grep('SELECT')), 1);
     R::useWriterCache(FALSE);
     // Test find cache
     $logger->clear();
     $book = R::find('book');
     $book = R::find('book');
     asrt(count($logger->grep('SELECT')), 2);
     // With cache
     R::getWriter()->setUseCache(TRUE);
     $logger->clear();
     $book = R::find('book');
     $book = R::find('book');
     asrt(count($logger->grep('SELECT')), 1);
     R::getWriter()->setUseCache(FALSE);
     // Test combinations
     $logger->clear();
     $book = R::findOne('book', ' id = ? ', array($id));
     $book->ownPage;
     R::batch('book', array($id));
     $book = R::findOne('book', ' id = ? ', array($id));
     $book->ownPage;
     R::batch('book', array($id));
     asrt(count($logger->grep('SELECT')), 6);
     // With cache
     R::getWriter()->setUseCache(TRUE);
     $logger->clear();
     R::batch('book', array($id));
     $book = R::findOne('book', ' id = ? ', array($id));
     $book->ownPage;
     $book = R::findOne('book', ' id = ? ', array($id));
     $book->ownPage;
     asrt(count($logger->grep('SELECT')), 3);
     R::getWriter()->setUseCache(FALSE);
     // Test auto flush
     $logger->clear();
     $book = R::findOne('book');
     $book->name = 'X';
     R::store($book);
     $book = R::findOne('book');
     asrt(count($logger->grep('SELECT *')), 2);
     // With cache
     R::getWriter()->setUseCache(TRUE);
     $logger->clear();
     $book = R::findOne('book');
     $book->name = 'Y';
     // Will flush
     R::store($book);
     $book = R::findOne('book');
     // Now the same, auto flushed
     asrt(count($logger->grep('SELECT *')), 2);
     R::getWriter()->setUseCache(FALSE);
     // Test whether delete flushes as well (because uses selectRecord - might be a gotcha!)
     R::store(R::dispense('garbage'));
     $garbage = R::findOne('garbage');
     $logger->clear();
     $book = R::findOne('book');
     R::trash($garbage);
     $book = R::findOne('book');
     asrt(count($logger->grep('SELECT *')), 2);
     R::store(R::dispense('garbage'));
     $garbage = R::findOne('garbage');
     // With cache
     R::getWriter()->setUseCache(TRUE);
     $logger->clear();
     $book = R::findOne('book');
     R::trash($garbage);
     $book = R::findOne('book');
     // Now the same, auto flushed
     asrt(count($logger->grep('SELECT *')), 2);
     R::getWriter()->setUseCache(FALSE);
     R::store(R::dispense('garbage'));
     $garbage = R::findOne('garbage');
     // With cache
     R::getWriter()->setUseCache(TRUE);
     $logger->clear();
     $book = R::findOne('book');
     R::getWriter()->queryRecord('garbage', array('id' => array($garbage->id)));
     $book = R::findOne('book');
     // Now the same, auto flushed
     asrt(count($logger->grep('SELECT *')), 2);
     $page = R::dispense('page');
     $book->sharedPage[] = $page;
     R::store($book);
     $logger->clear();
     $link = R::getWriter()->queryRecordLink('book', 'page', $book->id, $page->id);
     asrt(count($logger->grep('SELECT')), 1);
     $link = R::getWriter()->queryRecordLink('book', 'page', $book->id, $page->id);
     asrt(count($logger->grep('SELECT')), 1);
     R::getWriter()->setUseCache(FALSE);
     $link = R::getWriter()->queryRecordLink('book', 'page', $book->id, $page->id);
     asrt(count($logger->grep('SELECT')), 2);
     R::getWriter()->setUseCache(TRUE);
 }
Beispiel #5
0
 /**
  * Test common Facade usage scenarios.
  *
  * @return void
  */
 public function testCommonUsageFacade()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     asrt(R::getRedBean() instanceof OODB, TRUE);
     asrt(R::getToolBox() instanceof ToolBox, TRUE);
     asrt(R::getDatabaseAdapter() instanceof Adapter, TRUE);
     asrt(R::getWriter() instanceof QueryWriter, TRUE);
     $book = R::dispense("book");
     asrt($book instanceof OODBBean, TRUE);
     $book->title = "a nice book";
     $id = R::store($book);
     asrt($id > 0, TRUE);
     $book = R::load("book", (int) $id);
     asrt($book->title, "a nice book");
     asrt(R::load('book', 999)->title, NULL);
     R::freeze(TRUE);
     try {
         R::load('bookies', 999);
         fail();
     } catch (\Exception $e) {
         pass();
     }
     R::freeze(FALSE);
     $author = R::dispense("author");
     $author->name = "me";
     R::store($author);
     $book9 = R::dispense("book");
     $author9 = R::dispense("author");
     $author9->name = "mr Nine";
     $a9 = R::store($author9);
     $book9->author_id = $a9;
     $bk9 = R::store($book9);
     $book9 = R::load("book", $bk9);
     $author = R::load("author", $book9->author_id);
     asrt($author->name, "mr Nine");
     R::trash($author);
     R::trash($book9);
     pass();
     $book2 = R::dispense("book");
     $book2->title = "second";
     R::store($book2);
     $book3 = R::dispense("book");
     $book3->title = "third";
     R::store($book3);
     asrt(count(R::find("book")), 3);
     asrt(count(R::findAll("book")), 3);
     asrt(count(R::findAll("book", " LIMIT 2")), 2);
     asrt(count(R::find("book", " id=id ")), 3);
     asrt(count(R::find("book", " title LIKE ?", array("third"))), 1);
     asrt(count(R::find("book", " title LIKE ?", array("%d%"))), 2);
     // Find without where clause
     asrt(count(R::findAll('book', ' order by id')), 3);
     R::trash($book3);
     R::trash($book2);
     asrt(count(R::getAll("SELECT * FROM book ")), 1);
     asrt(count(R::getCol("SELECT title FROM book ")), 1);
     asrt((int) R::getCell("SELECT 123 "), 123);
     $book = R::dispense("book");
     $book->title = "not so original title";
     $author = R::dispense("author");
     $author->name = "Bobby";
     R::store($book);
     $aid = R::store($author);
     $author = R::findOne("author", " name = ? ", array("Bobby"));
 }
Beispiel #6
0
 /**
  * Test Facade bind function method.
  * Test for MySQL WKT spatial format.
  */
 public function testFunctionFilters()
 {
     R::nuke();
     R::bindFunc('read', 'location.point', 'asText');
     R::bindFunc('write', 'location.point', 'GeomFromText');
     R::store(R::dispense('location'));
     R::freeze(true);
     try {
         R::find('location');
         fail();
     } catch (SQL $exception) {
         pass();
     }
     R::freeze(false);
     try {
         R::find('location');
         pass();
     } catch (SQL $exception) {
         fail();
     }
     $location = R::dispense('location');
     $location->point = 'POINT(14 6)';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'point');
     $location = $location->fresh();
     asrt($location->point, 'POINT(14 6)');
     R::nuke();
     $location = R::dispense('location');
     $location->point = 'LINESTRING(0 0,1 1,2 2)';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'linestring');
     $location->bustcache = 2;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point, 'LINESTRING(0 0,1 1,2 2)');
     R::nuke();
     $location = R::dispense('location');
     $location->point = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))';
     R::store($location);
     $columns = R::inspect('location');
     asrt($columns['point'], 'polygon');
     $location->bustcache = 4;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point, 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))');
     R::bindFunc('read', 'location.point', NULL);
     $location->bustcache = 1;
     R::store($location);
     $location = $location->fresh();
     asrt($location->point === 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))', FALSE);
     $filters = AQueryWriter::getSQLFilters();
     asrt(is_array($filters), TRUE);
     asrt(count($filters), 2);
     asrt(isset($filters[QueryWriter::C_SQLFILTER_READ]), TRUE);
     asrt(isset($filters[QueryWriter::C_SQLFILTER_WRITE]), TRUE);
     R::bindFunc('read', 'place.point', 'asText');
     R::bindFunc('write', 'place.point', 'GeomFromText');
     R::bindFunc('read', 'place.line', 'asText');
     R::bindFunc('write', 'place.line', 'GeomFromText');
     R::nuke();
     $place = R::dispense('place');
     $place->point = 'POINT(13.2 666.6)';
     $place->line = 'LINESTRING(9.2 0,3 1.33)';
     R::store($place);
     $columns = R::inspect('place');
     asrt($columns['point'], 'point');
     asrt($columns['line'], 'linestring');
     $place = R::findOne('place');
     asrt($place->point, 'POINT(13.2 666.6)');
     asrt($place->line, 'LINESTRING(9.2 0,3 1.33)');
     R::bindFunc('read', 'place.point', NULL);
     R::bindFunc('write', 'place.point', NULL);
     R::bindFunc('read', 'place.line', NULL);
     R::bindFunc('write', 'place.line', NULL);
 }
Beispiel #7
0
function accueil()
{
    global $twig, $base, $titre;
    $personnes = R::find("personnes");
    return $twig->render("accueil.html", compact("base", "titre", "personnes"));
}
Beispiel #8
0
 /**
  * Test many different scenarios with self referential
  * many-to-many relations.
  *
  * @return void
  */
 public function testSelfReferentialCRUD()
 {
     R::nuke();
     $buddies = R::dispense('buddy', 4);
     $buddies[0]->name = 'A';
     $buddies[1]->name = 'B';
     $buddies[2]->name = 'C';
     $buddies[3]->name = 'D';
     $buddies[0]->sharedBuddyList = array($buddies[1], $buddies[2]);
     $buddies[3]->sharedBuddyList = array($buddies[2]);
     R::storeAll($buddies);
     $buddies[0] = $buddies[0]->fresh();
     asrt(count($buddies[0]->sharedBuddyList), 2);
     //does this yield valid combinations - cross relations / self ref n-m
     //need to symmetric....
     $names = R::gatherLabels($buddies[0]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'B,C');
     unset($buddies[0]->sharedBuddy);
     R::storeAll($buddies);
     $buddies[0] = $buddies[0]->fresh();
     asrt(count($buddies[0]->sharedBuddyList), 2);
     $buddies[3] = $buddies[3]->fresh();
     asrt(count($buddies[3]->sharedBuddyList), 1);
     $names = R::gatherLabels($buddies[3]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'C');
     $buddies[2] = $buddies[2]->fresh();
     asrt(count($buddies[2]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[2]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'A,D');
     $buddies[1] = $buddies[1]->fresh();
     asrt(count($buddies[1]->sharedBuddyList), 1);
     $names = R::gatherLabels($buddies[1]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'A');
     //Can we add one?
     $buddies[1]->sharedBuddyList[] = R::dispense('buddy')->setAttr('name', 'E');
     R::store($buddies[1]);
     $buddies[0] = $buddies[0]->fresh();
     asrt(count($buddies[0]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[0]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'B,C');
     $buddies[1] = $buddies[1]->fresh();
     asrt(count($buddies[1]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[1]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'A,E');
     $all = R::find('buddy');
     asrt(count($all), 5);
     foreach ($buddies[1]->sharedBuddy as $buddy) {
         if ($buddy->name === 'E') {
             $buddyE = $buddy;
         }
     }
     asrt(isset($buddyE), TRUE);
     asrt($buddyE->name, 'E');
     //can we update?
     foreach ($buddies[0]->sharedBuddy as $buddy) {
         if ($buddy->name === 'C') {
             $buddy->name = 'C2';
         }
     }
     R::store($buddies[0]);
     $buddies[0] = $buddies[0]->fresh();
     asrt(count($buddies[0]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[0]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'B,C2');
     $buddies[2] = $buddies[2]->fresh();
     asrt(count($buddies[2]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[2]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'A,D');
     //can we delete?
     foreach ($buddies[0]->sharedBuddyList as $id => $buddy) {
         if ($buddy->name === 'B') {
             unset($buddies[0]->sharedBuddyList[$id]);
         }
     }
     R::store($buddies[0]);
     $buddies[0] = $buddies[0]->fresh();
     asrt(count($buddies[0]->sharedBuddyList), 1);
     $names = R::gatherLabels($buddies[0]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'C2');
     $buddies[1] = $buddies[1]->fresh();
     asrt(count($buddies[1]->sharedBuddyList), 1);
     $names = R::gatherLabels($buddies[1]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'E');
     asrt(R::count('buddy'), 5);
     asrt(R::count('buddyBuddy'), 3);
     $buddies[3] = $buddies[3]->fresh();
     asrt(count($buddies[3]->sharedBuddyList), 1);
     $names = R::gatherLabels($buddies[3]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'C2');
     $buddies[2] = $buddies[2]->fresh();
     asrt(count($buddies[2]->sharedBuddyList), 2);
     $names = R::gatherLabels($buddies[2]->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'A,D');
     $buddyE = $buddyE->fresh();
     asrt(count($buddyE->sharedBuddyList), 1);
     $names = R::gatherLabels($buddyE->sharedBuddyList);
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'B');
     //can we access linked_by -- o dear mysql again! cant have alias in WHERE!
     if ($this->currentlyActiveDriverID === 'mysql') {
         $buddyE = $buddyE->fresh();
         asrt(count($buddyE->with(' HAVING linked_by > 0 ')->sharedBuddyList), 1);
         $buddyE = $buddyE->fresh();
         asrt(count($buddyE->with(' HAVING linked_by < 0 ')->sharedBuddyList), 0);
     }
     //even postgres sucks. Only SQLite knows how to handle this.
     //I dont give a f**k whether it's an SQL standard or not, it should just work.
     if ($this->currentlyActiveDriverID === 'sqlite') {
         $buddyE = $buddyE->fresh();
         asrt(count($buddyE->withCondition(' linked_by > 0 ')->sharedBuddyList), 1);
         $buddyE = $buddyE->fresh();
         asrt(count($buddyE->withCondition(' linked_by < 0 ')->sharedBuddyList), 0);
     }
     $buddyE = $buddyE->fresh();
     asrt(count($buddyE->withCondition(' buddy_buddy.buddy_id > 0 AND buddy_buddy.buddy2_id > 0 ')->sharedBuddyList), 1);
     $buddyE = $buddyE->fresh();
     asrt(count($buddyE->withCondition(' buddy_buddy.buddy_id < 0 AND buddy_buddy.buddy2_id < 0 ')->sharedBuddyList), 0);
 }
Beispiel #9
0
 /**
  * Test Full fluid UUID support.
  *
  */
 public function testFullSupport()
 {
     //Rewire objects to support UUIDs.
     $oldToolBox = R::getToolBox();
     $oldAdapter = $oldToolBox->getDatabaseAdapter();
     $uuidWriter = new \UUIDWriterPostgres($oldAdapter);
     $newRedBean = new OODB($uuidWriter);
     $newToolBox = new ToolBox($newRedBean, $oldAdapter, $uuidWriter);
     R::configureFacadeWithToolbox($newToolBox);
     list($mansion, $rooms, $ghosts, $key) = R::dispenseAll('mansion,room*3,ghost*4,key');
     $mansion->name = 'Haunted Mansion';
     $mansion->xownRoomList = $rooms;
     $rooms[0]->name = 'Green Room';
     $rooms[1]->name = 'Red Room';
     $rooms[2]->name = 'Blue Room';
     $ghosts[0]->name = 'zero';
     $ghosts[1]->name = 'one';
     $ghosts[2]->name = 'two';
     $ghosts[3]->name = 'three';
     $rooms[0]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[1]);
     $rooms[1]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[2]);
     $rooms[2]->noLoad()->sharedGhostList = array($ghosts[1], $ghosts[3], $ghosts[2]);
     $rooms[2]->xownKey = array($key);
     //Can we store a bean hierachy with UUIDs?
     R::debug(1);
     $id = R::store($mansion);
     //exit;
     asrt(is_string($id), TRUE);
     asrt(strlen($id), 36);
     $haunted = R::load('mansion', $id);
     asrt($haunted->name, 'Haunted Mansion');
     asrt(is_string($haunted->id), TRUE);
     asrt(strlen($haunted->id), 36);
     asrt(is_array($haunted->xownRoomList), TRUE);
     asrt(count($haunted->ownRoom), 3);
     $rooms = $haunted->xownRoomList;
     //Do some counting...
     $greenRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Green Room') {
             $greenRoom = $room;
             break;
         }
     }
     asrt(!is_null($greenRoom), TRUE);
     asrt(is_array($greenRoom->with(' ORDER BY id ')->sharedGhostList), TRUE);
     asrt(count($greenRoom->sharedGhostList), 2);
     $names = array();
     foreach ($greenRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,zero');
     $rooms = $haunted->xownRoomList;
     $blueRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Blue Room') {
             $blueRoom = $room;
             break;
         }
     }
     asrt(!is_null($blueRoom), TRUE);
     asrt(is_array($blueRoom->sharedGhostList), TRUE);
     asrt(count($blueRoom->sharedGhostList), 3);
     $names = array();
     foreach ($blueRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,three,two');
     $rooms = $haunted->xownRoomList;
     $redRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Red Room') {
             $redRoom = $room;
             break;
         }
     }
     $names = array();
     foreach ($redRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'two,zero');
     asrt(!is_null($redRoom), TRUE);
     asrt(is_array($redRoom->sharedGhostList), TRUE);
     asrt(count($redRoom->sharedGhostList), 2);
     //Can we repaint a room?
     $redRoom->name = 'Yellow Room';
     $id = R::store($redRoom);
     $yellowRoom = R::load('room', $id);
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 2);
     //Can we throw one ghost out?
     array_pop($yellowRoom->sharedGhost);
     R::store($yellowRoom);
     $yellowRoom = $yellowRoom->fresh();
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 1);
     //can we remove one of the rooms?
     asrt(R::count('key'), 1);
     $list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
     $room = reset($list);
     unset($mansion->xownRoomList[$room->id]);
     R::store($mansion);
     asrt(R::count('room'), 2);
     //and what about its dependent beans?
     asrt(R::count('key'), 0);
     asrt(R::count('ghost_room'), 3);
     //and can we find ghosts?
     $ghosts = R::find('ghost');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
     asrt(count($ghosts), 2);
     $ghostZero = R::findOne('ghost', ' "name" = ? ', array('zero'));
     asrt($ghostZero instanceof OODBBean, TRUE);
     //can we create link properties on existing tables?
     $blackRoom = R::dispense('room');
     $blackRoom->name = 'Black Room';
     $ghostZero->link('ghost_room', array('mood' => 'grumpy'))->room = $blackRoom;
     R::store($ghostZero);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->sharedRoomList;
     asrt(count($list), 3);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
     asrt(count($list), 1);
     //can we load a batch?
     $ids = R::getCol('SELECT id FROM ghost');
     $ghosts = R::batch('ghost', $ids);
     asrt(count($ghosts), 4);
     //can we do an aggregation?
     $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
     asrt(count($ghosts), 2);
     //can we duplicate the mansion?
     asrt(R::count('mansion'), 1);
     asrt(R::count('room'), 3);
     asrt(R::count('ghost'), 4);
     $copy = R::dup($mansion);
     R::store($copy);
     asrt(R::count('mansion'), 2);
     asrt(R::count('room'), 5);
     //black room does not belong to mansion 1
     asrt(R::count('ghost'), 4);
     //can we do some counting using the list?
     asrt($copy->countOwn('room'), 2);
     $rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
     $room = reset($rooms);
     asrt($room->countShared('ghost'), 2);
     //Finally restore old toolbox
     R::configureFacadeWithToolbox($oldToolBox);
 }
function deletePackageCost($id)
{
    $packageCost = R::find('packagecost', 'id=?', [$id]);
    R::trashAll($packageCost);
}
 /**
  * @param int $startIndex
  * @param int $numItems
  * @return array
  */
 public function getsystemlogs_paged($startIndex, $numItems, $userid = NULL)
 {
     try {
         $rows = R::find($this->tbname, ' LIMIT ? , ? ', array($startIndex, $numItems));
         $rows = R::prepareForAMF(R::convertBeanToArray($rows), array(0 => $this->tbname));
         return $rows;
     } catch (Exception $e) {
         throw new Exception($e->getMessage());
     }
 }
Beispiel #12
0
 /**
  * Test error handling of SQL states.
  *
  * @return void
  */
 public function testFindError()
 {
     R::freeze(FALSE);
     $page = R::dispense('page');
     $page->title = 'abc';
     R::store($page);
     //Column does not exist, in fluid mode no error!
     try {
         R::find('page', ' xtitle = ? ', array('x'));
         pass();
     } catch (SQL $e) {
         fail();
     }
     //Table does not exist, in fluid mode no error!
     try {
         R::find('pagex', ' title = ? ', array('x'));
         pass();
     } catch (SQL $e) {
         fail();
     }
     //Syntax error, error in fluid mode if possible to infer from SQLSTATE (MySQL/Postgres)
     try {
         R::find('page', ' invalid SQL ');
         //In SQLite only get HY000 - not very descriptive so suppress more errors in fluid mode then.
         if ($this->currentlyActiveDriverID === 'sqlite' || $this->currentlyActiveDriverID === 'CUBRID') {
             pass();
         } else {
             fail();
         }
     } catch (SQL $e) {
         pass();
     }
     //Frozen, always error...
     R::freeze(TRUE);
     //Column does not exist, in frozen mode error!
     try {
         R::find('page', ' xtitle = ? ', array('x'));
         fail();
     } catch (SQL $e) {
         pass();
     }
     //Table does not exist, in frozen mode error!
     try {
         R::find('pagex', ' title = ? ', array('x'));
         fail();
     } catch (SQL $e) {
         pass();
     }
     //Syntax error, in frozen mode error!
     try {
         R::find('page', ' invalid SQL ');
         fail();
     } catch (SQL $e) {
         pass();
     }
     R::freeze(FALSE);
 }
Beispiel #13
0
 /**
  * Test exporting with filters.
  *
  * @return void
  */
 public function ExportWithFilters()
 {
     testpack('Export with filters');
     $book = R::dispense('book');
     $pages = R::dispense('page', 2);
     $texts = R::dispense('text', 2);
     $images = R::dispense('image', 2);
     $author = R::dispense('author');
     $pub = R::dispense('publisher');
     $bookmarks = R::dispense('bookmark', 2);
     $pages[0]->ownText = array($texts[0]);
     $pages[0]->ownImage = array($images[0]);
     $pages[1]->ownText = array($texts[1]);
     $pages[1]->ownImage = array($images[1]);
     $pages[0]->sharedBookmark[] = $bookmarks[0];
     $pages[1]->sharedBookmark[] = $bookmarks[1];
     $bookmarks[0]->ownNote[] = R::dispense('note')->setAttr('text', 'a note');
     $bookmarks[1]->ownNote[] = R::dispense('note')->setAttr('text', 'a note');
     $book->ownPage = $pages;
     $book->author = $author;
     $author->publisher = $pub;
     $bookID = R::store($book);
     R::getDuplicationManager()->setTables(R::getWriter()->getTables());
     $objects = R::exportAll(array($book), TRUE, array());
     asrt(isset($objects[0]['ownPage']), TRUE);
     asrt(count($objects[0]['ownPage']), 2);
     asrt(isset($objects[0]['author']), TRUE);
     asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE);
     asrt(count($objects[0]['ownPage'][0]['ownText']), 1);
     asrt(isset($objects[0]['ownPage'][0]['ownImage']), TRUE);
     asrt(count($objects[0]['ownPage'][0]['ownImage']), 1);
     $objects = R::exportAll(array($book), TRUE, array('page', 'author', 'text', 'image'));
     asrt(isset($objects[0]['ownPage']), TRUE);
     asrt(count($objects[0]['ownPage']), 2);
     asrt(isset($objects[0]['author']), TRUE);
     asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE);
     asrt(count($objects[0]['ownPage'][0]['ownText']), 1);
     asrt(isset($objects[0]['ownPage'][0]['ownImage']), TRUE);
     asrt(count($objects[0]['ownPage'][0]['ownImage']), 1);
     $objects = R::exportAll(array($book), TRUE, 'author');
     asrt(isset($objects[0]['ownPage']), FALSE);
     asrt(isset($objects[0]['ownPage'][0]['ownText']), FALSE);
     $objects = R::exportAll(array($book), TRUE, array('page'));
     asrt(isset($objects[0]['author']), FALSE);
     asrt(isset($objects[0]['ownPage'][0]['ownText']), FALSE);
     $objects = R::exportAll(array($book), TRUE, array('page', 'text'));
     asrt(isset($objects[0]['author']), FALSE);
     asrt(isset($objects[0]['ownPage']), TRUE);
     asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE);
     asrt(count($objects[0]['ownPage'][0]['ownText']), 1);
     asrt(isset($objects[0]['ownPage'][0]['ownImage']), FALSE);
     $objects = R::exportAll(array($book), TRUE, array('none'));
     asrt(isset($objects[0]['author']), FALSE);
     asrt(isset($objects[0]['ownPage']), FALSE);
     $texts = R::find('text');
     R::getDuplicationManager()->setCacheTables(FALSE);
     testpack('Keyless export');
     $book = R::load('book', $bookID);
     $book->ownPage;
     $export = $book->export();
     asrt(isset($export['ownPage'][0]), TRUE);
 }
Beispiel #14
0
    $lines = R::exportAll(R::find('line', 'WHERE start_point_id=' . $city[0]['id']));
    $timetableCityDay = array();
    foreach ($lines as $line) {
        $timetable = R::exportAll(R::find('timetable', 'WHERE line_id=' . $line['id'] . ' AND day=' . $day));
        var_dump($timetable);
        if (!empty($timetable)) {
            $timetableCityDay[] = array($timetable[0]['start_time'], $line['end_point_id']);
        }
    }
    echo json_encode($timetableCityDay);
});
//return all the cities with coordinates
$app->get('/city', function () {
    $city = R::exportAll(R::find('city'));
    echo json_encode($city);
});
//Return line,price,start_time and distance by name city departure and name city arrival
$app->get('/nextStart/:cityNameDeparture/:cityNameArrival', function ($cityNameDeparture, $cityNameArrival) {
    $cityDeparture = R::exportAll(R::find('city', 'WHERE name="' . $cityNameDeparture . '"'));
    $cityArrival = R::exportAll(R::find('city', 'WHERE name="' . $cityNameArrival . '"'));
    $lines = R::exportAll(R::find('line', 'WHERE start_point_id=' . $cityDeparture[0]['id'] . ' AND end_point_id=' . $cityArrival[0]['id']));
    $timetableTrip = array();
    foreach ($lines as $line) {
        $timetable = R::exportAll(R::find('timetable', 'WHERE line_id=' . $line['id']));
        if (!empty($timetable)) {
            $timetableTrip[] = array($timetable[0]['start_time'], $line['price'], $line['distance'], $line['end_point_id']);
        }
    }
    echo json_encode($timetableTrip);
});
$app->run();