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