getAssoc() public static méthode

Executes SQL. Results will be returned as an associative array. The first column in the select clause will be used for the keys in this array and the second column will be used for the values. If only one column is selected in the query, both key and value of the array will have the value of this field for each row.
public static getAssoc ( string $sql, array $bindings = [] ) : array
$sql string SQL query to execute
$bindings array a list of values to be bound to query parameters
Résultat array
Exemple #1
0
 /**
  * Test beautification of column names.
  *
  * @return void
  */
 public function testBeautifulColumnNames()
 {
     testpack('Beautiful column names');
     $town = R::dispense('town');
     $town->isCapital = FALSE;
     $town->hasTrainStation = TRUE;
     $town->name = 'BeautyVille';
     $houses = R::dispense('house', 2);
     $houses[0]->isForSale = TRUE;
     $town->ownHouse = $houses;
     R::store($town);
     $town = R::load('town', $town->id);
     asrt($town->isCapital == FALSE, TRUE);
     asrt($town->hasTrainStation == TRUE, TRUE);
     asrt($town->name == 'BeautyVille', TRUE);
     testpack('Accept datetime objects.');
     $cal = R::dispense('calendar');
     $cal->when = new \DateTime('2000-01-01', new \DateTimeZone('Pacific/Nauru'));
     asrt($cal->when, '2000-01-01 00:00:00');
     testpack('Affected rows test');
     $currentDriver = $this->currentlyActiveDriverID;
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $bean = $redbean->dispense('bean');
     $bean->prop = 3;
     //make test run with strict mode as well
     $redbean->store($bean);
     $adapter->exec('UPDATE bean SET prop = 2');
     asrt($adapter->getAffectedRows(), 1);
     testpack('Testing Logger');
     R::getDatabaseAdapter()->getDatabase()->setLogger(new RDefault());
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof Logger, TRUE);
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof RDefault, TRUE);
     $bean = R::dispense('bean');
     $bean->property = 1;
     $bean->unsetAll(array('property'));
     asrt($bean->property, NULL);
     asrt($bean->setAttr('property', 2) instanceof OODBBean, TRUE);
     asrt($bean->property, 2);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d/', R::isoDate()), 1);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d/', R::isoDateTime()), 1);
     $redbean = R::getRedBean();
     $adapter = R::getDatabaseAdapter();
     $writer = R::getWriter();
     asrt($redbean instanceof OODB, TRUE);
     asrt($adapter instanceof Adapter, TRUE);
     asrt($writer instanceof QueryWriter, TRUE);
     R::setRedBean($redbean);
     pass();
     //cant really test this
     R::setDatabaseAdapter($adapter);
     pass();
     //cant really test this
     R::setWriter($writer);
     pass();
     //cant really test this
     $u1 = R::dispense('user');
     $u1->name = 'Gabor';
     $u1->login = '******';
     $u2 = R::dispense('user');
     $u2->name = 'Eric';
     $u2->login = '******';
     R::store($u1);
     R::store($u2);
     $list = R::getAssoc('select login,' . R::getWriter()->esc('name') . ' from ' . R::getWriter()->esc('user') . ' ');
     asrt($list['e'], 'Eric');
     asrt($list['g'], 'Gabor');
     $painting = R::dispense('painting');
     $painting->name = 'Nighthawks';
     $id = R::store($painting);
     testpack('Testing SQL Error Types');
     foreach ($writer->typeno_sqltype as $code => $text) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     foreach ($writer->sqltype_typeno as $text => $code) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     testpack('Testing Nowhere Pt. 1 (unfrozen)');
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         R::$method('select * from nowhere');
         pass();
     }
     testpack('Testing Nowhere Pt. 2 (frozen)');
     R::freeze(TRUE);
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         try {
             R::$method('select * from nowhere');
             fail();
         } catch (SQL $e) {
             pass();
         }
     }
     R::freeze(FALSE);
 }
Exemple #2
0
 /**
  * Test selecting.
  * 
  * @return void
  */
 public function testSelects()
 {
     $rooms = R::dispense('room', 2);
     $rooms[0]->kind = 'suite';
     $rooms[1]->kind = 'classic';
     $rooms[0]->number = 6;
     $rooms[1]->number = 7;
     R::store($rooms[0]);
     R::store($rooms[1]);
     $rooms = R::getAssoc('SELECT * FROM room WHERE id < -999');
     asrt(is_array($rooms), TRUE);
     asrt(count($rooms), 0);
     $rooms = R::getAssoc('SELECT ' . R::getWriter()->esc('number') . ', kind FROM room ORDER BY kind ASC');
     foreach ($rooms as $key => $room) {
         asrt($key === 6 || $key === 7, TRUE);
         asrt($room == 'classic' || $room == 'suite', TRUE);
     }
     $rooms = R::getDatabaseAdapter()->getAssoc('SELECT kind FROM room');
     foreach ($rooms as $key => $room) {
         asrt($room == 'classic' || $room == 'suite', TRUE);
         asrt($room, $key);
     }
     $rooms = R::getAssoc('SELECT `number`, kind FROM rooms2 ORDER BY kind ASC');
     asrt(count($rooms), 0);
     asrt(is_array($rooms), TRUE);
     // GetCell should return NULL in case of exception
     asrt(NULL, R::getCell('SELECT dream FROM fantasy'));
 }