public $db
The Mongo database (a MongoDB object). Set in the constructor
via MongoManager::get_database ();
public $is_new = false
Keeps track of whether the current object is new and needs to be inserted or updated on save.
public __construct ($vals = false, $is_new = true)
If $vals
is false, we're creating a new object from scratch.
If it contains an array, it's a new object from an array.
If $is_new
is false, then the array is an existing field
(mainly used internally by fetch()
).
If $vals
contains a single value, the object is retrieved from the database.
public set_database ($conn)
Allows you to inject the database connection into $db
. Also sets $collection
.
public _id ($id = false)
Returns a MongoId object from a regular ID value or if it's already a MongoId value, the original is returned.
public keyval ($id = false)
Returns the ID value from a MongoId object, or the original value if it's not a MongoId object.
public put ()
Save the object to the database. If $verify is set, it will validate the data against any rules in the array, or in the specified INI file if $verify is a string matching a file name.
public static query ($fields = false)
Begin a new query. Resets the internal state for a new query. Optionally you can pass the fields you want to return in the query, so you can optimize and not return them all.
public order ($order)
Order the query by the specified clauses. Specify each as:
field1 asc, field2 desc
public group ($keys, $initial, $reduce, $options)
Group the query by the specific clauses and immediately return
the results as a data structure. The group()
function in MongoDB
works much differently than GROUP BY
in SQL databases. For
more info, see:
http://www.php.net/manual/en/mongocollection.group.php
Unlike the group()
method in SQL-based models, this method
returns the results immediately. For example:
Data structure:
{ category: 1, name: "John" }
{ category: 1, name: "Steve" }
{ category: 2, name: "Adam" }
Query:
<?php
$res = MyModel::query ()->group (
array ('category' => 1), // keys
array ('items' => array ()), // initial
'function (obj, prev) { prev.items.push (obj.name); }' // reduce
);
?>
Results:
{
retval: [
{
category: 1,
items: [
name: "John",
name: "Steve"
]
},
{
category: 2,
items: [
name: "Adam"
]
}
],
count: 3,
keys: 2,
ok: 1
}
public where ($key, $val)
Add a where condition to the query. This is a field/value combo, and special values are allowed, such as:
->where ('age' => array ('$gt', 18))
public fetch_orig ($limit = false, $offset = 0)
Fetch as an array of the original objects as returned from the database.
public fetch_assoc ($key, $value, $limit = false, $offset = 0)
Fetch as an associative array of the specified key/value fields.
A class you can extend to create model objects in your application that write to a MongoDB collection in the back-end. Assumes collection and class name are identical, but that the collection is lowercase. Assumes primary key field is named
'_id'
. The collection can be changed by specifying a customname
property. Note that this class doesn't impose field names. It provides an easy way to get at MongoDB collections using the same pattern as regular SQL-based Model objects, and can be extended to encapsulate your logic around that data.Usage:
Also supports validation of values via:
Or specified as an INI file:
See
Form::verify_values
for more info on validation rules and file formats.Differences from Model objects:
keyval()
method for retrieving the unique ID value and ensuring it's not aMongoId
object.