Usage:
list( $book, $page, $text ) = R::dispenseAll( 'book,page,text' );
This will dispense a book, a page and a text. This way you can
quickly dispense beans of various types in just one line of code.
Usage:
list($book, $pages) = R::dispenseAll('book,page*100');
This returns an array with a book bean and then another array
containing 100 page beans. /** * Test whether we can't add more than one FK. */ public function testDuplicateFK() { R::nuke(); list($book, $page) = R::dispenseAll('book,page'); $book->sharedPage[] = $page; R::store($page); R::store($book); $added = R::getWriter()->addConstraintForTypes('page', 'book'); asrt($added, FALSE); }
/** * Test whether we can't add more than one FK. */ public function testDuplicateFK() { R::nuke(); list($book, $page) = R::dispenseAll('book,page'); $book->sharedPage[] = $page; R::store($page); R::store($book); $writer = R::getWriter(); $added1 = $writer->addFK('book_page', 'book', 'book_id', 'id', TRUE); $added2 = $writer->addFK('book_page', 'page', 'page_id', 'id', TRUE); $added = $added1 && $added2; asrt($added, FALSE); }
/** * Tests automatic resolvement of parent beans * without fetchAs() using inferFetchType (foreign keys). * * @return void */ public function testAutoResolver() { R::nuke(); list($project, $teacher, $student) = R::dispenseAll('project,person,person'); $teacher->name = 'teacher'; $student->name = 'student'; $project->teacher = $teacher; $project->student = $student; R::store($project); $project = $project->fresh(); asrt($project->teacher instanceof OODBBean, TRUE); asrt($project->student instanceof OODBBean, TRUE); asrt($project->teacher->name, 'teacher'); asrt($project->student->name, 'student'); $project2 = R::dispense('project'); $teacher2 = R::dispense('person'); $teacher2->name = 'teacher2'; $project2->teacher = $teacher2; R::store($project2); $project2 = $project2->fresh(); asrt($project2->teacher instanceof OODBBean, TRUE); asrt($project2->teacher->name, 'teacher2'); asrt(is_null($project2->student), TRUE); $project = $project->fresh(); asrt($project->fetchAs('person')->teacher instanceof OODBBean, TRUE); asrt($project->fetchAs('person')->student instanceof OODBBean, TRUE); asrt($project->fetchAs('person')->teacher->name, 'teacher'); asrt($project->fetchAs('person')->student->name, 'student'); $project = $project->fresh(); $export = R::exportAll(array($project), TRUE); asrt(isset($export[0]['teacher']['name']), TRUE); asrt(isset($export[0]['student']['name']), TRUE); asrt($export[0]['teacher']['name'], 'teacher'); asrt($export[0]['student']['name'], 'student'); //Also test the default implementation... $nullWriter = new \NullWriter(R::getDatabaseAdapter()); asrt(is_null($nullWriter->inferFetchType('test', 'test')), TRUE); //Realteacher should take precedence over fetchAs-teacher, name conventions first! //also: ensure we do not use autoresolv for anything except when truly necessary! (performance) $realTeacher = R::dispense('teacher'); $realTeacher->name = 'real'; R::store($realTeacher); //ID must be same asrt($realTeacher->id, $teacher->id); $project = $project->fresh(); asrt($project->teacher->name, 'real'); }
/** * Tests the facade-only dispenseAll method. * * @return void */ public function testDispenseAll() { list($book, $page) = Facade::dispenseAll('book,page'); asrt($book instanceof OODBBean, TRUE); asrt($page instanceof OODBBean, TRUE); asrt($book->getMeta('type'), 'book'); asrt($page->getMeta('type'), 'page'); list($book, $page, $texts, $mark) = R::dispenseAll('book,page,text*2,mark'); asrt($book instanceof OODBBean, TRUE); asrt($page instanceof OODBBean, TRUE); asrt(is_array($texts), TRUE); asrt($mark instanceof OODBBean, TRUE); asrt($book->getMeta('type'), 'book'); asrt($page->getMeta('type'), 'page'); asrt($mark->getMeta('type'), 'mark'); asrt($texts[0]->getMeta('type'), 'text'); asrt($texts[1]->getMeta('type'), 'text'); list($eggs, $milk, $butter) = R::dispenseAll('eggs*3,milk*1,butter*9'); asrt(count($eggs), 3); asrt($milk instanceof OODBBean, TRUE); asrt(count($butter), 9); list($eggs, $milk, $butter) = R::dispenseAll('eggs*3,milk*1,butter*9', TRUE); asrt(count($eggs), 3); asrt(count($milk), 1); asrt(count($eggs), 3); list($beer) = R::dispenseAll('beer*0', TRUE); asrt(is_array($beer), TRUE); asrt(count($beer), 0); list($beer) = R::dispenseAll('beer*0', FALSE); asrt(is_array($beer), FALSE); asrt(is_null($beer), TRUE); asrt(count($beer), 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); }
/** * Only fire update query if the bean really contains different * values. But make sure beans several 'parents' away still get * saved. * * @return void */ public function testBeanTainting() { $logger = R::getDatabaseAdapter()->getDatabase()->getLogger(); list($i, $k, $c, $s) = R::dispenseAll('invoice,customer,city,state'); $i->customer = $k; $i->status = 0; $k->city = $c; $c->state = $s; $s->name = 'x'; R::store($i); $i = $i->fresh(); asrt($i->customer->city->state->name, 'x'); $i->status = 1; R::freeze(true); $logger = R::debug(1, 1); //do we properly skip unmodified but tainted parent beans? R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); asrt(count($numberOfUpdateQueries), 1); //does cascade update still work? $i = $i->fresh(); $i->customer->city->state->name = 'y'; R::store($i); $i = $i->fresh(); asrt($i->customer->city->state->name, 'y'); $i = $i->fresh(); $differentCity = R::dispense('city'); R::store($differentCity); $i->customer->city = $differentCity; R::store($i); $i = $i->fresh(); asrt($i->customer->city->id != $c->id, TRUE); asrt(is_null($i->customer->city->state), TRUE); $i->customer->city = NULL; R::store($i); $i = $i->fresh(); asrt(is_null($i->customer->city), TRUE); $i->customer = $k; $i->status = 0; $k->city = $c; $c->state = $s; $s->name = 'x'; R::store($i); R::freeze(FALSE); $i = $i->fresh(); //can we still change remote parent? $i->customer->city->name = 'q'; $logger->clear(); R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); //print_r($logger->getLogs()); asrt(count($numberOfUpdateQueries), 1); $i = $i->fresh(); asrt($i->customer->city->name, 'q'); //do we properly skip unmodified but tainted parent beans? $i->status = 3; $logger->clear(); R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); asrt(count($numberOfUpdateQueries), 1); }
public function testErrorHandling() { R::nuke(); list($book, $page) = R::dispenseAll('book,page'); $book->sharedPage[] = $page; R::store($page); $redbean = R::getRedBean(); $am = $redbean->getAssociationManager(); //SQLite and CUBRID do not comply with ANSI SQLState codes. $catchAll = $this->currentlyActiveDriverID == 'sqlite' || $this->currentlyActiveDriverID === 'CUBRID'; try { $am->related($book, 'page', 'invalid SQL'); if ($catchAll) { pass(); } else { fail(); } } catch (SQL $e) { if ($catchAll) { fail(); } else { pass(); } } try { $am->related($book, 'cover'); pass(); } catch (SQL $e) { fail(); } try { $am->related(R::dispense('cover'), 'book'); pass(); } catch (SQL $e) { fail(); } }
/** * Test findLike. * * @return void */ public function testFindLike2() { list($flowers, $shop) = R::dispenseAll('flower*4,shop'); $flowers[0]->color = 'red'; $flowers[1]->color = 'yellow'; $flowers[2]->color = 'blue'; $flowers[3]->color = 'purple'; $flowers[0]->price = 10; $flowers[1]->price = 15; $flowers[2]->price = 20; $flowers[3]->price = 25; $shop->xownFlowerList = $flowers; R::store($shop); asrt($this->getColors(R::findLike('flower', array('color' => array('red', 'yellow')), ' price < 20')), 'red,yellow'); asrt($this->getColors(R::findLike('flower', array('color' => array()), '')), 'blue,purple,red,yellow'); asrt($this->getColors(R::findLike('flower', array('color' => array()))), 'blue,purple,red,yellow'); asrt($this->getColors(R::findLike('flower', array('color' => array('blue')), ' OR price = 25')), 'blue,purple'); asrt($this->getColors(R::findLike('flower', array('color' => array()), ' price < 25')), 'blue,red,yellow'); asrt($this->getColors(R::findLike('flower', array('color' => array()), ' price < 20')), 'red,yellow'); asrt($this->getColors(R::findLike('flower', array('color' => array()), ' ORDER BY color DESC'), TRUE), 'yellow,red,purple,blue'); asrt($this->getColors(R::findLike('flower', array('color' => array()), ' ORDER BY color LIMIT 1')), 'blue'); asrt($this->getColors(R::findLike('flower', array('color' => array('yellow', 'blue')), ' ORDER BY color ASC LIMIT 1')), 'blue'); }