pdo_009.phpt 2.5 KB

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