xref: /PHP-8.2/ext/pdo/tests/pdo_010.phpt (revision 902d6439)
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 $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
64$stmt->execute();
65var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_GROUP, 'Test3'));
66
67$stmt->execute();
68var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_UNIQUE, 'Test3'));
69
70?>
71--EXPECTF--
72Test1::__construct()
73Test2::__construct()
74Test3::__construct()
75array(2) {
76  ["Group1"]=>
77  array(2) {
78    [0]=>
79    object(stdClass)#%d (2) {
80      ["id"]=>
81      string(1) "1"
82      ["val"]=>
83      string(1) "A"
84    }
85    [1]=>
86    object(Test1)#%d (2) {
87      ["id"]=>
88      string(1) "2"
89      ["val"]=>
90      string(1) "B"
91    }
92  }
93  ["Group2"]=>
94  array(2) {
95    [0]=>
96    object(Test2)#%d (2) {
97      ["id"]=>
98      string(1) "3"
99      ["val"]=>
100      string(1) "C"
101    }
102    [1]=>
103    object(Test3)#%d (2) {
104      ["id"]=>
105      string(1) "4"
106      ["val"]=>
107      string(1) "D"
108    }
109  }
110}
111Test1::__construct()
112Test2::__construct()
113Test3::__construct()
114array(2) {
115  ["Group1"]=>
116  object(Test1)#%d (2) {
117    ["id"]=>
118    string(1) "2"
119    ["val"]=>
120    string(1) "B"
121  }
122  ["Group2"]=>
123  object(Test3)#%d (2) {
124    ["id"]=>
125    string(1) "4"
126    ["val"]=>
127    string(1) "D"
128  }
129}
130