xref: /PHP-8.1/ext/pdo/tests/pdo_009.phpt (revision 74859783)
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 function __construct()
33    {
34        echo __METHOD__ . "()\n";
35    }
36}
37
38class Test2
39{
40    public function __construct()
41    {
42        echo __METHOD__ . "()\n";
43    }
44}
45
46class Test3
47{
48    public function __construct()
49    {
50        echo __METHOD__ . "()\n";
51    }
52}
53
54$stmt->execute();
55var_dump($stmt->fetchAll(PDO::FETCH_NUM));
56
57$stmt->execute();
58var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE, 'Test3'));
59
60?>
61--EXPECTF--
62array(4) {
63  [0]=>
64  array(3) {
65    [0]=>
66    string(8) "stdClass"
67    [1]=>
68    string(1) "1"
69    [2]=>
70    string(1) "A"
71  }
72  [1]=>
73  array(3) {
74    [0]=>
75    string(5) "Test1"
76    [1]=>
77    string(1) "2"
78    [2]=>
79    string(1) "B"
80  }
81  [2]=>
82  array(3) {
83    [0]=>
84    string(5) "Test2"
85    [1]=>
86    string(1) "3"
87    [2]=>
88    string(1) "C"
89  }
90  [3]=>
91  array(3) {
92    [0]=>
93    NULL
94    [1]=>
95    string(1) "4"
96    [2]=>
97    string(1) "D"
98  }
99}
100Test1::__construct()
101Test2::__construct()
102Test3::__construct()
103array(4) {
104  [0]=>
105  object(stdClass)#%d (2) {
106    ["id"]=>
107    string(1) "1"
108    ["val"]=>
109    string(1) "A"
110  }
111  [1]=>
112  object(Test1)#%d (2) {
113    ["id"]=>
114    string(1) "2"
115    ["val"]=>
116    string(1) "B"
117  }
118  [2]=>
119  object(Test2)#%d (2) {
120    ["id"]=>
121    string(1) "3"
122    ["val"]=>
123    string(1) "C"
124  }
125  [3]=>
126  object(Test3)#%d (2) {
127    ["id"]=>
128    string(1) "4"
129    ["val"]=>
130    string(1) "D"
131  }
132}
133