- Improved the Blog model sample
[activemongo.git] / sample / blog / Author.php
blob5029ff700533d944abe88c3498aa679b729c17e2
1 <?php
3 class AuthorModel extends ActiveMongo
5 public $username;
6 public $name;
8 function getCollectionName()
10 return 'author';
13 /**
14 * Username filter.
16 * - It must be unique (handled by MongoDB actually).
17 * - It can't be changed.
18 * - It must be /[a-z][a-z0-9\-\_]+/
19 * - It must be longer than 5 letters.
21 * @return bool
23 function username_filter($value, $old_value)
25 if ($old_value!=null && $value != $old_value) {
26 throw new FilterException("The username can't be changed");
29 if (!preg_match("/[a-z][a-z0-9\-\_]+/", $value)) {
30 throw new FilterException("The username is not valid");
33 if (strlen($value) < 5) {
34 throw new Exception("Username too short");
37 return true;
40 /**
41 * When an User updates his profile, we need to
42 * make sure that every post written by him is also
43 * updated with his name and username.
45 * @return void
47 function on_update()
49 $post = new PostModel;
50 $post->updateAuthorInfo($this->getID());
53 function setup()
55 $collection = & $this->_getCollection();
56 $collection->ensureIndex(array('username' => 1), array('unique'=> 1, 'background' => 1));