Skip to content

Commit 8b45f9f

Browse files
committed
Merge pull request #74 from foxycode/master
Added fetchField function to Selection
2 parents 930abd6 + 34c0172 commit 8b45f9f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Database/Table/Selection.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
});

0 commit comments

Comments
 (0)