purekid / mongodm

MongoDB ORM that includes support for references,embed and multilevel inheritance.
MIT License
200 stars 47 forks source link

Lazy model update/remove #68

Open jrschumacher opened 10 years ago

jrschumacher commented 10 years ago

To improve speed to updating data I propose an optional model when updating data to lazily update by use of the findAndModify command.

Note: This option would make the assumption that you are content with the data you are saving. That is to say, when it gets executed it will replace data as specified.

Prototype

<?php

  // Goal:
  // 1. db.Person.findAndModify({_id: ObjectId('12345')}, {$set: {name: 'John Smith', age: 30}})
  // 2. Consistant API

  // defaults: sort = [], update = true, new = true
  $options = array(
    'sort' => array(),
    ['update' => true | 'remove' => true],
    'new' => true,
    'fields' => array(),
    'upsert' => false
  );

  Person::lazy($options); // or Person::findAndModify();
  $p = Person::id('12345');
  $p->name = 'John Smith';
  $p->age = 30;
  $p->save();

  //-- OR --

  // Create new methods (via trait) 
  $p = Person::pId('12345', $options);
  $p->name = 'John Smith';
  $p->age = 30;
  $p->save();

The choices we have from my perspective is either create some magic to set a flag for lazy/findAndModify/etc mode or clone existing functions.

jrschumacher commented 10 years ago

I personally like the first option, but this has its downsides as it could lead to unintended consequences if the flag isn't removed in time.