- Changed sample directory name
[activemongo.git] / samples / user.php
blob64e7241c0d8fab5607489444824935898de71095
1 <?php
2 /*
3 +----------------------------------------------------------------------+
4 | Copyright (c) 2009 The PHP Group |
5 +----------------------------------------------------------------------+
6 | This source file is subject to version 3.0 of the PHP license, |
7 | that is bundled with this package in the file LICENSE, and is |
8 | available through the world-wide-web at the following url: |
9 | http://www.php.net/license/3_0.txt. |
10 | If you did not receive a copy of the PHP license and are unable to |
11 | obtain it through the world-wide-web, please send a note to |
12 | license@php.net so we can mail you a copy immediately. |
13 +----------------------------------------------------------------------+
14 | Authors: Cesar Rodas <crodas@php.net> |
15 +----------------------------------------------------------------------+
18 class Users Extends ActiveMongo
20 public $username;
21 public $password;
22 public $uid;
24 function my_selector()
26 /* Get collection */
27 $col = $this->_getCollection();
29 /* Build our request */
30 $res = $col->find(array('uid' => array('$gt' => 5, '$lt' => 10)));
31 $res->sort(array('uid' => -1));
33 /* Give to ActiveMongo our Cursor */
34 $this->setCursor($res);
36 /* You must return 'this' for easy iteration */
37 return $this;
40 function username_filter(&$value, $past_value)
42 if ($past_value != null && $value != $past_value) {
43 throw new FilterException("You cannot change the username");
44 } else {
45 if (strlen($value) < 5) {
46 throw new FilterException("Name too short");
49 return true;
52 function password_filter(&$value, $past_value)
54 if (strlen($value) < 5) {
55 throw new FilterException("Password is too sort");
57 $value = sha1($value);
58 return true;
61 function setup()
63 $this->_getCollection()->ensureIndex(array('uid' => 1), array('unique' => true, 'background' => true));
66 function mapreduce()
68 $col = self::_getConnection();
69 $data = array(
70 'mapreduce' => 'users',
71 'map' => new MongoCode("emit(this.karma, this.uid);"),
72 'reduce' => new MongoCode("return 1;"),
73 'verbose' => true,
74 'out' => 'salida',
76 $salida = $col->command($data);
77 var_dump($salida);