File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,26 @@ public function fetch()
223223 }
224224
225225
226+ /**
227+ * Fetches single field.
228+ * @param string|NULL
229+ * @return mixed|FALSE
230+ */
231+ public function fetchField ($ column = NULL )
232+ {
233+ if ($ column ) {
234+ $ this ->select ($ column );
235+ }
236+
237+ $ row = $ this ->fetch ();
238+ if ($ row ) {
239+ return $ column ? $ row [$ column ] : array_values ($ row ->toArray ())[0 ];
240+ }
241+
242+ return FALSE ;
243+ }
244+
245+
226246 /**
227247 * @inheritDoc
228248 */
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Test: Nette\Database\Table: Fetch field.
5+ * @dataProvider? ../databases.ini
6+ */
7+
8+ use Tester \Assert ;
9+
10+ require __DIR__ . '/../connect.inc.php ' ; // create $connection
11+
12+ Nette \Database \Helpers::loadFromFile ($ connection , __DIR__ . "/../files/ {$ driverName }-nette_test1.sql " );
13+
14+
15+ test (function () use ($ context ) {
16+ $ title = $ context ->table ('book ' )->where ('id ' , 1 )->fetchField ('title ' ); // SELECT `title` FROM `book` WHERE `id` = 1
17+ Assert::same ('1001 tipu a triku pro PHP ' , $ title );
18+ });
19+
20+
21+ test (function () use ($ context ) {
22+ $ title = $ context ->table ('book ' )->where ('id ' , 1 )->select ('title ' )->fetchField (); // SELECT `title` FROM `book` WHERE `id` = 1
23+ Assert::same ('1001 tipu a triku pro PHP ' , $ title );
24+ });
You can’t perform that action at this time.
0 commit comments