summaryrefslogtreecommitdiff
path: root/ext/pdo_mysql/tests/pdo_mysql_stmt_fetch_serialize_simple.phpt
blob: 890c7b73cb6c1f11dca9e752475396045e84bf29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--TEST--
MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
?>
--FILE--
<?php
	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
	$db = MySQLPDOTest::factory();

	try {

		class myclass implements Serializable {

			public function __construct($caller = null) {
				printf("%s(%s) - note that it must not be called when unserializing\n", __METHOD__, var_export($caller, true));
			}

			public function __set($prop, $value) {
				printf("%s(%s, %s)\n", __METHOD__, var_export($prop, true), var_export($value, true));
				$this->{$prop} = $value;
			}

			public function serialize() {
				printf("%s()\n", __METHOD__);
				return 'Value from serialize()';
			}

			public function unserialize($data) {
				printf("%s(%s)\n", __METHOD__, var_export($data, true));
			}

		}

		printf("Lets see what the Serializeable interface makes our object behave like...\n");
		$obj = new myclass('Called by script');
		$tmp = unserialize(serialize($obj));
		var_dump($tmp);

		printf("\nAnd now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)...\n");
		$db->exec('DROP TABLE IF EXISTS test');
		$db->exec(sprintf('CREATE TABLE test(myobj BLOB) ENGINE=%s', MySQLPDOTest::getTableEngine()));
		$db->exec("INSERT INTO test(myobj) VALUES ('Data fetched from DB to be given to unserialize()')");

		$stmt = $db->prepare('SELECT myobj FROM test');
		$stmt->execute();
		$rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO'));
		var_dump($rows[0]);

		$stmt->execute();
		$rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass');
		var_dump($rows[0]);

		printf("\nAnd now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()...\n");
		$stmt = $db->prepare('SELECT myobj FROM test');
		$stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO'));
		$stmt->execute();
		var_dump($stmt->fetch());

	} catch (PDOException $e) {
		printf("[001] %s [%s] %s\n",
			$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
	}

	$db->exec('DROP TABLE IF EXISTS test');
	print "done!\n";
?>
--EXPECTF--
Lets see what the Serializeable interface makes our object behave like...
myclass::__construct('Called by script') - note that it must not be called when unserializing
myclass::serialize()
myclass::unserialize('Value from serialize()')
object(myclass)#%d (0) {
}

And now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)...
myclass::unserialize('Data fetched from DB to be given to unserialize()')
object(myclass)#%d (0) {
}
myclass::unserialize('Data fetched from DB to be given to unserialize()')
object(myclass)#%d (0) {
}

And now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()...
myclass::unserialize('Data fetched from DB to be given to unserialize()')
object(myclass)#%d (0) {
}
done!