batch() public static method

important note: Because this method loads beans using the load() function (but faster) it will return empty beans with ID 0 for every bean that could not be located. The resulting beans will have the passed IDs as their keys.
public static batch ( string $type, array $ids ) : array
$type string type of beans
$ids array ids to load
return array
Esempio n. 1
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);
 }
Esempio n. 2
0
 /**
  * Test trashAll().
  */
 public function testMultiDeleteUpdate()
 {
     testpack('test multi delete and multi update');
     $beans = R::dispenseLabels('bean', array('a', 'b'));
     $ids = R::storeAll($beans);
     asrt((int) R::count('bean'), 2);
     R::trashAll(R::batch('bean', $ids));
     asrt((int) R::count('bean'), 0);
     testpack('test assocManager check');
     $rb = new OODB(R::getWriter());
     try {
         $rb->getAssociationManager();
         fail();
     } catch (RedException $e) {
         pass();
     }
 }
Esempio n. 3
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);
 }
Esempio n. 4
0
 /**
  * Test missing bean scenarios.
  *
  * @return void
  */
 public function testMissingBeans()
 {
     testpack('deal with missing beans');
     $id = R::store(R::dispense('beer'));
     $bottles = R::batch('beer', array($id, $id + 1, $id + 2));
     asrt(count($bottles), 3);
     asrt((int) $bottles[$id]->id, (int) $id);
     asrt((int) $bottles[$id + 1]->id, 0);
     asrt((int) $bottles[$id + 2]->id, 0);
 }
Esempio n. 5
0
 /**
  * Test whether batch still works if no IDs have been passed.
  * 
  * @return void
  */
 public function testBatch0()
 {
     $zero = R::batch('page', array());
     asrt(is_array($zero), TRUE);
     asrt(count($zero), 0);
     $zero = R::batch('page', FALSE);
     asrt(is_array($zero), TRUE);
     asrt(count($zero), 0);
     $zero = R::batch('page', NULL);
     asrt(is_array($zero), TRUE);
     asrt(count($zero), 0);
 }