$object->site = array(
'views' => 0,
'clicks' => 0,
'pages' => array(
'12' => array(
'views' => 0,
'clicks' => 0
)
)
); $object->site['pages']['12']['views']->increment(5); (this corresponds to a $inc => { site.pages.12.views : 5 } update) echo 'this site has' . $object->site['total'] . ' views';public function save()
{
return $this->loaded()
? $this->update()
: $this->create();
}Posted By: WouterYes they do. All you have to do is some commenting/uncommenting in classes/A1.php (Mango::factory and $user->update()) and replace user->id by user->_id
Posted By: nanodocumetI think it will take a little bit more than just changing that info
have you used Mango in any real-life web project?
<?php
class Model_Test extends Mango {
protected $_fields = array(
'some_array' => array('type'=>'array', 'type_hint'=>'array'),
);
protected $_db = 'demo'; //don't use default db config
}
$test = Mango::factory('test');
$test->some_array[] = array('key1' => '1500', 'key2' => '2500'); // Notice that values are set as strings
$test->create();
> db.tests.find()
{ "_id" : ObjectId("4b37b6da20ac5dbcfe000000"), "some_array" : [ { "key1" : "1500", "key2" : "2500" } ] }
$test = Mango::factory('test', array('_id' => '4b37b6da20ac5dbcfe000000'))->load();
$test->some_array[0] = array('key1' => 'test', 'key2' => 'test'); // Both need to be the same size to be updated
$test->update();
$test = Mango::factory('test', array('_id' => '4b37b6da20ac5dbcfe000000'))->load();
$test->some_array[0] = array('key1' => 'test1', 'key2' => '123');
$test->update();
<?php
class Model_Test extends Mango {
protected $_fields = array(
'some_array' => array('type'=>'array', 'type_hint'=>'array'),
'name' => array('type'=>'string')
);
protected $_db = 'demo'; //don't use default db config
}
$test = Mango::factory('test', array('_id' => '4b37b6da20ac5dbcfe000000'))->load();
$test->name = 'test';
$test->update();
$test = Mango::factory('test');
$test->name = 'test'; // Gets created
$test->create();
$test = Mango::factory('test');
$test->name = 'Test';
$test->some_array[] = array('key1' => 'AAAEFG', 'key2' => 'TEST');
$test->create();
$test1 = Mango::factory('test');
$test1->name = 'Test1';
$test1->create();
$test1->some_array[] = array('key1' => '123', 'key2' => 'TEST1');
$test1->update();
> db.tests.find()
{ "_id" : ObjectId("4b37c0f520ac5d28f9000000"), "some_array" : [ { "key1" : "AAAEFG", "key2" : "TEST" } ], "name" : "Test" }
{ "_id" : ObjectId("4b37c0f520ac5d28f9010000"), "name" : "Test1", "some_array" : { "0" : { "key1" : "123", "key2" : "TEST1" } } }
$test = Mango::factory('test', array('_id' => '4b37c0f520ac5d28f9000000'))->load();
$test->some_array[0] = array('key1' => 'AAAAAA', 'key2' => 'TEST');
$test->update();
$test1 = Mango::factory('test', array('_id' => '4b37c0f520ac5d28f9010000'))->load();
$test1->some_array[0] = array('key1' => '333', 'key2' => 'TESTY');
$test1->update();
{
_id : ObjectId('1234'),
some_array: [ 'value0', 'value1'],
some_object: {
key : 'value'
}
}<?php
class Model_Test extends Mango {
protected $_fields = array(
'some_set' => array('type'=>'set', 'type_hint'=>'array'),
'name' => array('type'=>'string')
);
protected $_db = 'demo'; //don't use default db config
}
-------+------------+----------------------
Mango | Mongo | PHP
-------+------------+----------------------
Set | Array | Numerical array
Array | Object | Associative array
-------+------------+----------------------
basic_set => array('type'=>'set');$model = Mango::factory('test');
$model->basic_set[] = 'value'; // equivalent of ->push()
$model->basic_set[1] = 'value1';
$model->basic_set->push('value2'); // equivalent of ->[] basic_array => array('type'=>'array');$model = Mango::factory('test');
$model->basic_array['key'] = 'value'; array_of_counters => array('type'=>'array', 'type_hint' => 'counter');$model = Mango::factory('test');
$model->basic_array['key_name']->increment(1); array_of_sets => array('type'=>'array', 'type_hint' => 'set');$model = Mango::factory('test');
$model->basic_array['key_name']->push('value1'); echo Kohana::debug($model->changed( $model->loaded()));class Model_Person extends Mango {
protected $_fields = array(
'siblings' => array('type'=>'set', 'type_hint'=>'array')
);
}
$person = Mango::factory('person');
$person->siblings[] = array('name' => 'Mary', 'last_name' => 'Smith');
$person->create();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0] = array('name' => 'Jane', 'last_name' => 'Smith');
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[] = array('name' => 'Mary', 'last_name' => 'Smith');
$person->create();
$person->siblings[0] = array('name' => 'Jane', 'last_name' => 'Smith'); //Even if values are EXACT size as original values
$person->update();
> db.persons.find()
{ "_id" : ObjectId(MongoId), "some_set" : [
{
"name" : "Mary",
"last_name" : "Smith"
},
{
"name" : "Jane",
"last_name" : "Smith"
}
] }
class Model_Person extends Mango {
protected $_fields = array(
'siblings' => array('type'=>'array', 'type_hint'=>'array')
);
}
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0] = array('name' => 'Jane', 'last_name' => 'Smith');
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0] = array('name' => 'Mary Jane', 'last_name' => 'Smith');
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0] = array('name' => 'Jane');
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0]['name'] = 'Jane';
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings[0]['middle_name'] = 'Jane';
$person->update();
$person = Mango::factory('person', array('_id' => MongoId))->load();
$person->siblings['eldest'] = array('name' => 'Mary', 'last_name' => 'Smith');
$person->create();
$person->siblings['eldest'] = array('name' => 'Mary Jane', 'last_name' => 'Smith');
$person->update();
$person->siblings['eldest']['name'] = 'Maria';
$person->update();
$person->siblings['eldest']['middle_name'] = 'Jane'; // Adding new key is possible
$person->update();
// Be careful when copying objects
$person->siblings['youngest'] = $person->siblings['eldest']; // Copying existing object
$person->siblings['youngest']['name'] = 'Joe'; // This updates the key 'name' for $person->siblings['eldest'] as well
$person->siblings['eldest']['name'] = 'Maryann'; // This updates the key 'name' for $person->siblings['youngest'] as well
$person->update();
Posted By: Wouter1) Updating sets using dotnotated array indeces doesn't work properly in Mongo. Bug is reported here: http://jira.mongodb.org/browse/SERVER-181
> db.example.remove();
> db.example.insert({_id:1, siblings : [ { name : 'jane', last_name : 'smith' }]});
> db.example.update({_id:1},{ $set : { 'siblings.0' : { name : 'joe' }}});
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
> db.example.update({_id:1},{ $set : { 'siblings.0.name' : 'joe' }});
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clauseclass Model_Person extends Mango {
protected $_fields = array(
'siblings' => array('type'=>'has_many')
);
}class Model_Sibling extends Mango {
protected $_embedded = TRUE;
protected $_fields = array(
'first_name' => array('type' => 'string'),
'last_name' => array('type' => 'string')
);
} $person = Mango::factory('person');
$sibling = Mango::factory('sibling', array(
'first_name' => 'mary',
'last_name' => 'smith'
));
$person->siblings[] = $sibling;
// $person->add($sibling); is also valid
$person->create();
$person = Mango::factory('person', array(
'_id' => $person->_id
))->load();
$person->siblings[0]->first_name = 'john';protected function _set_model_definition(array $definition)
{
if(isset($definition['_fields']))
{
$this->_fields = array_merge($this->_fields,$definition['_fields']);
}
if(isset($definition['_relations']))
{
$this->_relations = array_merge($this->_relations,$definition['_relations']);
}
}
/**
* Return the count of a (set of) document(s) from the database
*
* @param array specify additional criteria
* @return int the result from count
*/
public function count(array $criteria = array())
{
// Not sure if should restrict it to embedded
if($this->_embedded)
{
throw new Mango_Exception(':name model is embedded and cannot be loaded from database',
array(':name' => $this->_model));
}
$criteria += $this->changed(FALSE);
if ( isset($criteria['_id']))
{
// if ID is set, we don't need any other value
$criteria = array(
'_id' => $criteria['_id']
);
}
// resets $this->_changed array
$this->clear();
return $this->_db->count($this->_collection,$criteria);
}
public function count( $collection_name, array $query = array(), array $fields = array() )
{
return $this->_call('count', array(
'collection_name' => $collection_name,
'query' => $query,
'fields' => $fields
));
}
public function count( $collection_name, array $query = array())
{
return $this->_call('count', array(
'collection_name' => $collection_name,
'query' => $query
));
}
case 'count':
$r = $c->count($query,$fields);
case 'count':
$r = $c->count($query);
$visits = Mango::factory('log', array('date' => '2010-01-01'))->count();On another note, the MongoDB team just released 1.3 (unstable) which supports $set with array indices and $unset, so expect some updates soon!
$db = MangoDB::instance(); // or $log->db();
$visits = $db->count('logs', array('date'=>'2010-01-01')); $model = Mango::factory('test')->load(1);
unset($model->my_set[0]);
$model->update();
class Model_Test extends Mango {
protected $_fields = array(
'my_set' => array(
'type'=>'set'
)
);
}
It looks like you're new here. If you want to get involved, click one of these buttons!