xref: /PHP-7.4/ext/pdo/tests/pdo_009.phpt (revision 26dfce7f)
1--TEST--
2PDO Common: PDO::FETCH_CLASSTYPE
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo')) die('skip');
6$dir = getenv('REDIR_TEST_DIR');
7if (false == $dir) die('skip no driver');
8require_once $dir . 'pdo_test.inc';
9PDOTest::skip();
10?>
11--FILE--
12<?php
13if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
14require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
15$db = PDOTest::factory();
16
17$db->exec('CREATE TABLE classtypes(id int NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL UNIQUE)');
18$db->exec('INSERT INTO classtypes VALUES(0, \'stdClass\')');
19$db->exec('INSERT INTO classtypes VALUES(1, \'Test1\')');
20$db->exec('INSERT INTO classtypes VALUES(2, \'Test2\')');
21$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, classtype int, val VARCHAR(10))');
22$db->exec('INSERT INTO test VALUES(1, 0, \'A\')');
23$db->exec('INSERT INTO test VALUES(2, 1, \'B\')');
24$db->exec('INSERT INTO test VALUES(3, 2, \'C\')');
25$db->exec('INSERT INTO test VALUES(4, 3, \'D\')');
26
27$stmt = $db->prepare('SELECT classtypes.name, test.id AS id, test.val AS val FROM test LEFT JOIN classtypes ON test.classtype=classtypes.id');
28
29class Test1
30{
31	public function __construct()
32	{
33		echo __METHOD__ . "()\n";
34	}
35}
36
37class Test2
38{
39	public function __construct()
40	{
41		echo __METHOD__ . "()\n";
42	}
43}
44
45class Test3
46{
47	public function __construct()
48	{
49		echo __METHOD__ . "()\n";
50	}
51}
52
53$stmt->execute();
54var_dump($stmt->fetchAll(PDO::FETCH_NUM));
55
56$stmt->execute();
57var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE, 'Test3'));
58
59?>
60--EXPECTF--
61array(4) {
62  [0]=>
63  array(3) {
64    [0]=>
65    string(8) "stdClass"
66    [1]=>
67    string(1) "1"
68    [2]=>
69    string(1) "A"
70  }
71  [1]=>
72  array(3) {
73    [0]=>
74    string(5) "Test1"
75    [1]=>
76    string(1) "2"
77    [2]=>
78    string(1) "B"
79  }
80  [2]=>
81  array(3) {
82    [0]=>
83    string(5) "Test2"
84    [1]=>
85    string(1) "3"
86    [2]=>
87    string(1) "C"
88  }
89  [3]=>
90  array(3) {
91    [0]=>
92    NULL
93    [1]=>
94    string(1) "4"
95    [2]=>
96    string(1) "D"
97  }
98}
99Test1::__construct()
100Test2::__construct()
101Test3::__construct()
102array(4) {
103  [0]=>
104  object(stdClass)#%d (2) {
105    ["id"]=>
106    string(1) "1"
107    ["val"]=>
108    string(1) "A"
109  }
110  [1]=>
111  object(Test1)#%d (2) {
112    ["id"]=>
113    string(1) "2"
114    ["val"]=>
115    string(1) "B"
116  }
117  [2]=>
118  object(Test2)#%d (2) {
119    ["id"]=>
120    string(1) "3"
121    ["val"]=>
122    string(1) "C"
123  }
124  [3]=>
125  object(Test3)#%d (2) {
126    ["id"]=>
127    string(1) "4"
128    ["val"]=>
129    string(1) "D"
130  }
131}
132