1 | <?php |
||
2 | /** |
||
3 | * @author Todd Burry <[email protected]> |
||
4 | * @copyright 2009-2017 Vanilla Forums Inc. |
||
5 | * @license MIT |
||
6 | */ |
||
7 | |||
8 | namespace Garden\Db\Utils; |
||
9 | |||
10 | use PDO; |
||
11 | |||
12 | trait FetchModeTrait { |
||
13 | /** |
||
14 | * @var array The fetch arguments (if any). |
||
15 | */ |
||
16 | private $fetchArgs = [PDO::FETCH_ASSOC]; |
||
17 | |||
18 | /** |
||
19 | * Get the default fetch mode. |
||
20 | * |
||
21 | * @return int Returns the current fetch mode. |
||
22 | */ |
||
23 | 17 | public function getFetchMode() { |
|
24 | 17 | return reset($this->fetchArgs); |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Get the fetch arguments. |
||
29 | * |
||
30 | * @return array Returns an array of arguments including the fetch mode. |
||
31 | */ |
||
32 | 107 | public function getFetchArgs() { |
|
33 | 107 | return $this->fetchArgs; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * Set the default fetch mode. |
||
38 | * |
||
39 | * @param int|string $mode One of the **PDO::FETCH_*** constants or a class name. |
||
40 | * @param mixed ...$args Additional arguments for {@link \PDOStatement::fetchAll()}. |
||
41 | * @return $this |
||
42 | * @see http://php.net/manual/en/pdostatement.fetchall.php |
||
43 | */ |
||
44 | 17 | public function setFetchMode($mode, ...$args) { |
|
45 | 17 | if (is_string($mode)) { |
|
46 | array_unshift($args, PDO::$mode); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
47 | $mode = PDO::FETCH_CLASS; |
||
48 | } |
||
49 | 17 | $this->fetchArgs = array_merge([$mode], $args); |
|
50 | |||
51 | 17 | return $this; |
|
52 | } |
||
53 | } |
||
54 |