/
address_supercolumn.php
95 lines (72 loc) · 2.3 KB
/
address_supercolumn.php
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
92
93
94
95
<?php
/**
* (c) 2010 phpgrease.net
*
* @author Michael Pearson <pandra-support@phpgrease.net>
*/
/**
* Example dynamic Address Book construct using Column Family (Super) and Super Columns
*/
error_reporting(E_ALL);
require_once(dirname(__FILE__).'/../config.php');
if (!PandraCore::auto('localhost')) {
die(PandraCore::$lastError);
}
// hard code the row key for this example
$keyID = 'PAddressStore';
// default keyspace in storage.conf
$keySpace = 'Keyspace1';
// default Super Column name
$columnFamilyName = 'Super1';
class Address extends PandraSuperColumn {
public function init() {
$this->addColumn('city', 'string');
$this->addColumn('street', 'string');
$this->addColumn('zip', 'int');
}
}
class Addresses extends PandraSuperColumnFamily {
public function init() {
global $keySpace, $columnFamilyName;
$this->setKeySpace($keySpace);
$this->setName($columnFamilyName);
$this->addSuper(new Address('homeAddress'));
$this->addSuper(new Address('workAddress'));
}
}
$addrs = new Addresses();
$addrs->setKeyID($keyID);
// home address
$homeAddr = $addrs->getColumn('homeAddress');
$homeAddr->setColumn('city', 'san francisco');
$homeAddr->setColumn('street', '1234 x street');
$homeAddr->setColumn('zip', '94107');
// work address
$workAddr = $addrs->getColumn('workAddress');
$workAddr->setColumn('city', 'san jose');
$workAddr->setColumn('street', '9876 y drive');
// custom labelled supercolumn
$customAddr = new Address('customAddress');
$customAddr->setColumn('city', 'another city');
$addrs->addSuper($customAddr);
if (!$addrs->save()) {
die($addrs->lastError());
}
unset($addrs);
// Load the saved Addresses
$addrs = new Addresses();
if (!$addrs->load($keyID)) {
die($addrs->lastError());
}
// Show our loaded CF in JSON format, including key path
echo '<pre>'.$addrs->toJSON(TRUE).'</pre>';
// Example export -> import of JSON structure from loaded data into new object
$addrsClone = new Addresses();
// Populates data from JSON export (therefore must exclude key path)
$addrsClone->populate($addrs->toJSON());
// Set key
$addrsClone->setKeyID($keyID);
// Show the JSON for our CF with cloned data. This and the prior echo should match
echo '<pre>'.$addrsClone->toJSON(TRUE).'</pre>';
PandraCore::disconnectAll();
?>