itemNo = $itemNo; $this->itemName = $itemName; $this->itemPrice = $itemPrice; $this->quantity = $quantity; } public function getItemNo() { return $this->itemNo; } public function getItemName() { return $this->itemName; } public function getItemPrice() { return $this->itemPrice; } public function getQuantity() { return $this->quantity; } public function getPrice() { return bcadd((string)$this->quantity, $this->itemPrice); } } class Order { private $orderId; private $customer; private $items; public function __construct($orderId, $customer) { $this->orderId = $orderId; $this->customer = $customer; $this->items = array(); } public function getOrderId() { return $this->orderId; } public function getCustomer() { return $this->customer; } public function &getItems() { return $this->items; } public function getPrice() { $res = '0.00'; foreach($this->items as $ol) { $res = bcadd($res, $ol->getPrice()); } return $res; } } function serializeOrderLine($ol) { return array('itemNo' => $ol->getItemNo(), 'itemName' => $ol->getItemName(), 'itemPrice' => $ol->getItemPrice(), 'quantity' => $ol->getQuantity()); } function serializeOrder($o) { $a = array(); foreach($o->getItems() as $ol) { $a[] = serializeOrderLine($ol); } return array('orderId' => $o->getOrderId(), 'customer' => $o->getCustomer(), 'items' => $a); } function deserializeOrderLine($d) { return new OrderLine($d['itemNo'], $d['itemName'], $d['itemPrice'], $d['quantity']); } function deserializeOrder($d) { $res = new Order($d['orderId'], $d['customer']); foreach($d['items'] as $d1) { $res->getItems()[] = deserializeOrderLine($d1); } return $res; } function dump($col, $doc) { echo 'dumping ' . json_encode($doc) . "\r\n"; $da = $col->find($doc)->toArray(); foreach($da as $d) { //echo json_encode($d); $o = deserializeOrder($d); echo sprintf("Id : %d\r\n", $o->getOrderId()); echo sprintf("Customer : %s\r\n", $o->getCustomer()); echo sprintf("Price : %s\r\n", $o->getPrice()); foreach($o->getItems() as $ol) { echo sprintf(" %2d %-30s %10s %4d %10s\r\n", $ol->getItemNo(), $ol->getItemName(), $ol->getItemPrice(), $ol->getQuantity(), $ol->getPrice()); } } } // open $client = new Client('mongodb://localhost:27017'); $db = $client->TestDB; $col = $db->php_order; // setup $o1 = new Order(1, 'A A'); $o1->getItems()[] = new OrderLine(1, 'A good PHP book', '19.95', 1); $col->insertOne(serializeOrder($o1)); $o2 = new Order(2, 'B B'); $o2->getItems()[] = new OrderLine(1, 'ASUS MB', '249.00', 1); $o2->getItems()[] = new OrderLine(2, 'i5 CPU', '299.00', 1); $o2->getItems()[] = new OrderLine(3, '4 GB kit', '29.00', 4); $col->insertOne(serializeOrder($o2)); // query $d = array(); // all dump($col, $d); $d = array('orderId' => 2); // where id=2 dump($col, $d); $d = array('customer' => 'A A'); // where customer='A A' dump($col, $d); $d = array('customer' => array('$regex' => 'A.*'));// where customer like 'A%' dump($col, $d); $d = array('items.itemName' => 'i5 CPU'); // where items.name='i5 CPU' dump($col, $d); $d = array('items.itemName' => array('$regex' => '.*PHP.*')); // where items.name like '%Java%' dump($col, $d); $d = array('orderId' => array('$in' => array(1, 2))); // where id in (1,2) dump($col, $d); ?>