예제 #1
0
 /**
  * run a raw sql query
  *
  * @param string          $sql
  * @param array           $queryParams
  * @param Connection|null $connection
  *
  * @return bool|int|Rows
  */
 public static function query(string $sql, $queryParams = [], Connection $connection = null)
 {
     if ($connection === null) {
         self::connect();
         $connection = self::$connection;
     }
     $result = $connection->execute($sql, $queryParams);
     // format the data if it was a select
     if ($result && !empty($result->num_rows)) {
         // create a container for the rows
         $rows = new Rows();
         // put all rows in the collection
         foreach ($result as $row) {
             // add a new instance of this row to the collection
             $rows->add_row((new static($row))->set_loaded_from_database(true));
         }
     } else {
         if ($connection->get_affected_rows() >= 1) {
             $rows = $connection->get_affected_rows();
         } else {
             $rows = false;
         }
     }
     if (method_exists($result, 'free')) {
         $result->free();
     }
     return $rows;
 }
예제 #2
0
 public function test_arrayAccess()
 {
     $rows = new Rows();
     $numRows = 0;
     foreach (range(0, 10) as $i) {
         $rows->add_row(new Test(['name' => $i]));
         $numRows++;
     }
     $this->assertEquals(0, $rows[0]->name, "getting row by array index should work");
     ++$i;
     $this->assertFalse(isset($rows[$i]), "new offset shouldn't exist yet");
     $rows[$i] = new Test(['name' => $i]);
     $this->assertTrue(isset($rows[$i]), "setting an offset should work if it's a generic class");
     $rows[$i + 1] = new stdClass();
     $this->assertFalse(isset($rows[$i + 1]), "setting an offset should fail if it's not a generic class");
     ++$i;
     $this->assertFalse(isset($rows[$i]), "new offset shouldn't exist yet");
     $rows[] = new Test(['name' => $i]);
     $this->assertTrue(isset($rows[$i]), "appending to the rows class should append to the internal storage array");
     $this->assertEquals($i, $rows[$i]->name, "setting row by array index should work");
     unset($rows[$i]);
     $this->assertNull($rows[$i], "unset on rows should remove row from storage array");
 }