From aa111f06fc3cb58fb98a06a9d0748bf43acd3cf7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Wed, 3 Mar 2010 09:10:20 -0300 Subject: [PATCH] - Fixed minor bugs on ActiveMongo - Added a more complex example (borrowed from http://www.phpclasses.org/blog/post/118-Developing-scalable-PHP-applications-using-MongoDB.html) --- ActiveMongo.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++--- README | 14 +++++++-- sample/blog/App.php | 48 +++++++++++++++++++++++++++++ sample/blog/Author.php | 39 ++++++++++++++++++++++++ sample/blog/Post.php | 58 +++++++++++++++++++++++++++++++++++ 5 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 sample/blog/App.php create mode 100644 sample/blog/Author.php create mode 100644 sample/blog/Post.php diff --git a/ActiveMongo.php b/ActiveMongo.php index eb1dd3f..f61763b 100644 --- a/ActiveMongo.php +++ b/ActiveMongo.php @@ -118,6 +118,31 @@ abstract class ActiveMongo implements Iterator } // }}} + // void install() {{{ + /** + * Install. + * + * This static method iterate over the classes lists, + * and execute the setup() method on every ActiveMongo + * subclass. You should do this just once. + * + */ + final public static function install() + { + $classes = array_reverse(get_declared_classes()); + foreach ($classes as $class) + { + if ($class == 'ActiveMongo') { + break; + } + if (is_subclass_of($class, 'ActiveMongo')) { + $obj = new $class; + $obj->setup(); + } + } + } + // }}} + // void connection($db, $host) {{{ /** * Connect @@ -389,7 +414,7 @@ abstract class ActiveMongo implements Iterator final protected function setResult($obj) { /* Unsetting previous results, if any */ - foreach ((array)array_keys($this->_current) as $key) { + foreach (array_keys((array)$this->_current) as $key) { unset($this->$key); } @@ -403,7 +428,7 @@ abstract class ActiveMongo implements Iterator } // }}} - // this find() {{{ + // this find([$_id]) {{{ /** * Simple find * @@ -412,9 +437,12 @@ abstract class ActiveMongo implements Iterator * * @return object this */ - function find() + function find(MongoID $_id = null) { $vars = $this->getCurrentDocument(); + if ($_id != null) { + $vars['_id'] = $_id; + } $res = $this->_getCollection()->find($vars); $this->setCursor($res); return $this; @@ -458,7 +486,11 @@ abstract class ActiveMongo implements Iterator $this->_current = $obj; } /* post-save hook */ - $this->on_save(); + if ($update) { + $this->on_update(); + } else { + $this->on_save(); + } } // }}} @@ -556,6 +588,22 @@ abstract class ActiveMongo implements Iterator return $this->_cursor->rewind(); } // }}} + + // getID() {{{ + /** + * Return the current document ID. If there is + * no document it would return false. + * + * @return object|false + */ + final public function getID() + { + if ($this->_id instanceof MongoID) { + return $this->_id; + } + return false; + } + // }}} // string key() {{{ /** @@ -565,7 +613,7 @@ abstract class ActiveMongo implements Iterator */ final function key() { - return $this->_cursor->key(); + return $this->getID(); } // }}} @@ -602,6 +650,19 @@ abstract class ActiveMongo implements Iterator } // }}} + // void on_update() {{{ + /** + * On Update hook + * + * This method is fired right after an update is performed. + * + * @return void + */ + protected function on_update() + { + } + // }}} + // void on_iterate() {{{ /** * On Iterate Hook @@ -617,6 +678,17 @@ abstract class ActiveMongo implements Iterator } // }}} + // setup() {{{ + /** + * This method should contain all the indexes, and shard keys + * needed by the current collection. This try to make + * installation on development environments easier. + */ + function setup() + { + } + // }}} + } /* diff --git a/README b/README index 52830b7..41e72d6 100644 --- a/README +++ b/README @@ -1,7 +1,15 @@ Really simple ActiveRecord for MongoDB ---------------------------------------- -This class is under development, don't use it yet. +MongoDB is a very powerful database, very performant and amicable. I wrote an article about it, + +http://www.phpclasses.org/blog/post/118-Developing-scalable-PHP-applications-using-MongoDB.html + +ActiveMongo, is a simple yet efficient MongoDB abstraction, following the ActiveRecord pattern. Visit http://crodas.org/activemongo.php for further details. + + +Comments, patches, bug reports are welcome to the blog post, my personal e-mail address, crodas@php.net + What it does now: @@ -15,8 +23,10 @@ What it does now: - Support for efficient updates on nested documents - Support Hooks: - pre_save($operation, Array &$document): Right before to perform and insert or update - - on_save(): When a document is saved + - on_save(): When a document is created + - on_update(): When a document is updated - on_iterate(): On iteration, when the cursor moves to the next document +- Support simple yet useful collection installation, useful to create indexes and shard keys. TODO: - Add efficient support for references to another Record, and an efficient way to load it (with one query if possible) diff --git a/sample/blog/App.php b/sample/blog/App.php new file mode 100644 index 0000000..3d704e7 --- /dev/null +++ b/sample/blog/App.php @@ -0,0 +1,48 @@ +username = "crodas"; +$author->name = "Cesar Rodas"; +$author->save(); + +$post = new PostModel; +$post->uri = "/hello-world"; +$post->author = $author; +$post->save(); + +$post = new PostModel; +$post->uri = "/hello-world-1"; +$post->author = $author; +$post->save(); + +/* Clean up the current the resultset */ +$post->reset(); +$post->author = $author->getID(); +foreach ($post->find() as $bp) { + var_dump("Author: ".$bp->author_name); +} + +$author->name = "cesar d. rodas"; +$author->save(); + +var_dump("Author profile has been updated"); + +/* Clean up the current the resultset */ +$post->reset(); +$post->author = $author->getID(); +foreach ($post->find() as $bp) { + var_dump("Author: ".$bp->author_name); +} + +/* delete collections */ +$post->drop(); +$author->drop(); diff --git a/sample/blog/Author.php b/sample/blog/Author.php new file mode 100644 index 0000000..8269720 --- /dev/null +++ b/sample/blog/Author.php @@ -0,0 +1,39 @@ +updateAuthorInfo($this->getID()); + } + + function setup() + { + $collection = & $this->_getCollection(); + $collection->ensureIndex(array('username' => 1), array('unique'=> 1, 'background' => 1)); + } +} diff --git a/sample/blog/Post.php b/sample/blog/Post.php new file mode 100644 index 0000000..e5f96f9 --- /dev/null +++ b/sample/blog/Post.php @@ -0,0 +1,58 @@ +getID(); + } + } + + function getCollectionName() + { + return 'post'; + } + + function updateAuthorInfo(MongoID $id) + { + $author = new AuthorModel; + $author->find($id); + + $document = array( + '$set' => array( + 'author_name' => $author->name, + 'author_username' => $author->username, + ), + ); + + $filter = array( + 'author' => $id, + ); + + $this->_getCollection()->update($filter, $document, array('multiple' => true)); + + return true; + } + + function on_save() + { + $this->updateAuthorInfo($this->author); + } + + function setup() + { + $collection = & $this->_getCollection(); + $collection->ensureIndex(array('uri' => 1), array('unique'=> 1, 'background' => 1)); + } +} + + -- 2.11.4.GIT