Fetch
fetch( [int $limit] )
fetch( [int $offset, int $limit] )
The fetch() method is used to retrieve multiple records and has built in limiting. To fetch all records, you can simply call the fetch() method without a limit, or you can use the fetchAll() method.
To fetch a single record you can use fetch(1) or the fetchOne() method.
1 2 3 4 5 6 7 8 9 10 11 |
// Fetch All Records
DB::MySQL()->table('myTable')->select()->fetch(); // Fetch One Record
DB::MySQL()->table('myTable')->select()->fetch(1); // Fetch 10 Records
DB::MySQL()->table('myTable')->select()->fetch(10); // Fetch 10 Records with an Offset of 20
DB::MySQL()->table('myTable')->select()->fetch(20, 10); |
Note the fetch() method will return the results as Data Objects in an array, unless you set a limit of 1, which returns just the Data Object (not in an array).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Fetch All Records
$r = DB::MySQL()->table('myTable')->select()->fetch(); foreach($r as $t) { echo $t->name . '<br />'; } // Fetch One Record
$r = DB::MySQL()->table('myTable')->select()->fetch(1); echo $r->name . '<br />'; // Fetch 10 Records
DB::MySQL()->table('myTable')->select()->fetch(10); // Fetch 10 Records with an Offset of 20
DB::MySQL()->table('myTable')->select()->fetch(20, 10); |
Because the results are data objects, you can take advantage of Magic Data Manipulation and saving.
1 2 3 4 5 6 7 8 9 |
// Fetch and then Update Record
$r = DB::MySQL()->table('myTable')->select()->fetch(1); $r->name = 'new name'; $r->save(); // Update a specific record in result array
$r = DB::MySQL()->table('myTable')->select()->fetch(); $r[0]->name = 'new name'; $r[0]->save(); |
For more see Data Manipulation.