Skip to content

Commit a0788b3

Browse files
raicabogdanniden
authored andcommitted
Clarify when to use fetchMode constant
1 parent 76aa75d commit a0788b3

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Diff for: docs/db-layer.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -1136,14 +1136,21 @@ foreach ($invoices as $invoice) {
11361136
$invoice = $connection->fetchOne($sql);
11371137
```
11381138

1139-
By default, these calls create arrays with both associative and numeric indexes. You can change this behavior by using `Phalcon\Db\Result::setFetchMode()`. This method receives a constant, defining which kind of index is required.
1139+
By default, these calls create arrays with both associative and numeric indexes. For methods like
1140+
`fetchArray()`, `fetch()` and `dataSeek()` you can change this behavior by using `Phalcon\Db\Result::setFetchMode()`. For methods like `fetchAll()` or `fetchOne()` you can use the `$fetchMode` argument.
11401141

1141-
| Constant | Description |
1142-
|--------------------------------|-----------------------------------------------------------|
1143-
| `Phalcon\Db\Enum::FETCH_NUM` | Return an array with numeric indexes |
1144-
| `Phalcon\Db\Enum::FETCH_ASSOC` | Return an array with associative indexes |
1145-
| `Phalcon\Db\Enum::FETCH_BOTH` | Return an array with both associative and numeric indexes |
1146-
| `Phalcon\Db\Enum::FETCH_OBJ` | Return an object instead of an array |
1142+
The `fetchMode` receives a constant, defining which kind of index is required.
1143+
1144+
| Constant | Description |
1145+
|---------------------------------|------------------------------------------------------------|
1146+
| `Phalcon\Db\Enum::FETCH_NUM` | Return an array with numeric indexes |
1147+
| `Phalcon\Db\Enum::FETCH_ASSOC` | Return an array with associative indexes |
1148+
| `Phalcon\Db\Enum::FETCH_BOTH` | Return an array with both associative and numeric indexes |
1149+
| `Phalcon\Db\Enum::FETCH_GROUP` | Return an array of grouped associative and numeric indexes |
1150+
| `Phalcon\Db\Enum::FETCH_OBJ` | Return an object instead of an array |
1151+
| `Phalcon\Db\Enum::FETCH_COLUMN` | Returns a string for single row or array multiple rows |
1152+
1153+
There are many other constants that can be used similar to PDO:FETCH_* constants
11471154

11481155
```php
11491156
<?php
@@ -1166,6 +1173,16 @@ $result->setFetchMode(
11661173
while ($invoice = $result->fetch()) {
11671174
echo $invoice[0];
11681175
}
1176+
1177+
$invoices = $connection->fetchAll($sql, Phalcon\Db\Enum::FETCH_ASSOC);
1178+
// or using the previous query() method
1179+
$invoices = $result->fetchAll(Phalcon\Db\Enum::FETCH_ASSOC)
1180+
1181+
foreach ($invoices as $invoice) {
1182+
echo $invoice['inv_title'];
1183+
}
1184+
1185+
$invoice = $connection->fetchOne($sql, Phalcon\Db\Enum::FETCH_ASSOC);
11691186
```
11701187

11711188
The `query()` method returns an instance of [Phalcon\Db\Result\Pdo][db-result-pdo]. These objects encapsulate all the functionality related to the returned resultset i.e. traversing, seeking specific records, count etc.

0 commit comments

Comments
 (0)