pdo_009.phpt 2.5 KB

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