xref: /PHP-8.1/ext/pdo/tests/pdo_010.phpt (revision 74859783)
1--TEST--
2PDO Common: PDO::FETCH_CLASSTYPE and GROUP/UNIQUE
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), grp VARCHAR(10))');
23$db->exec('INSERT INTO test VALUES(1, 0, \'A\', \'Group1\')');
24$db->exec('INSERT INTO test VALUES(2, 1, \'B\', \'Group1\')');
25$db->exec('INSERT INTO test VALUES(3, 2, \'C\', \'Group2\')');
26$db->exec('INSERT INTO test VALUES(4, 3, \'D\', \'Group2\')');
27
28$stmt = $db->prepare('SELECT classtypes.name, test.grp AS grp, 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
55$stmt->execute();
56var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_GROUP, 'Test3'));
57
58$stmt->execute();
59var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_UNIQUE, 'Test3'));
60
61?>
62--EXPECTF--
63Test1::__construct()
64Test2::__construct()
65Test3::__construct()
66array(2) {
67  ["Group1"]=>
68  array(2) {
69    [0]=>
70    object(stdClass)#%d (2) {
71      ["id"]=>
72      string(1) "1"
73      ["val"]=>
74      string(1) "A"
75    }
76    [1]=>
77    object(Test1)#%d (2) {
78      ["id"]=>
79      string(1) "2"
80      ["val"]=>
81      string(1) "B"
82    }
83  }
84  ["Group2"]=>
85  array(2) {
86    [0]=>
87    object(Test2)#%d (2) {
88      ["id"]=>
89      string(1) "3"
90      ["val"]=>
91      string(1) "C"
92    }
93    [1]=>
94    object(Test3)#%d (2) {
95      ["id"]=>
96      string(1) "4"
97      ["val"]=>
98      string(1) "D"
99    }
100  }
101}
102Test1::__construct()
103Test2::__construct()
104Test3::__construct()
105array(2) {
106  ["Group1"]=>
107  object(Test1)#%d (2) {
108    ["id"]=>
109    string(1) "2"
110    ["val"]=>
111    string(1) "B"
112  }
113  ["Group2"]=>
114  object(Test3)#%d (2) {
115    ["id"]=>
116    string(1) "4"
117    ["val"]=>
118    string(1) "D"
119  }
120}
121