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