Called after an Entity is saved. Updates the object's internal state to reflect the
corresponding database entity, and sets the Entity object's key, if this is a newly-created
object. **Do not** call this method if you intend to update the database's copy of the
entity. Instead, see Model::save().
public sync ( mixed $id = null, array $data = [], array $options = [] ) | ||
$id | mixed | The ID to assign, where applicable. |
$data | array | Any additional generated data assigned to the object by the database. |
$options | array | Method options: - `'materialize'` _boolean_: Determines whether or not the flag should be set that indicates that this entity exists in the data store. Defaults to `true`. - `'dematerialize'` _boolean_: If set to `true`, indicates that this entity has been deleted from the data store and no longer exists. Defaults to `false`. |
public function testIncrement() { $entity = new Entity(array('data' => array('counter' => 0))); $this->assertEqual(0, $entity->counter); $entity->increment('counter'); $this->assertEqual(1, $entity->counter); $entity->decrement('counter', 5); $this->assertEqual(-4, $entity->counter); $this->assertNull($entity->increment); $entity->increment('foo'); $this->assertEqual(1, $entity->foo); $this->assertFalse(isset($entity->bar)); $entity->bar = 'blah'; $entity->sync(); $this->expectException("/^Field 'bar' cannot be incremented.\$/"); $entity->increment('bar'); }
public function testModified() { $entity = new Entity(); $this->assertEqual(array(), $entity->modified()); $data = array('foo' => 'bar', 'baz' => 'dib'); $entity->set($data); $this->assertEqual(array('foo' => true, 'baz' => true), $entity->modified()); $this->assertTrue($entity->modified('foo')); $this->assertTrue($entity->modified('baz')); /* and last, checking a non-existing field */ $this->assertNull($entity->modified('ole')); $subentity = new Entity(); $subentity->set($data); $entity->set(array('ble' => $subentity)); $this->assertEqual(array('foo' => true, 'baz' => true, 'ble' => true), $entity->modified()); $this->assertTrue($entity->ble->modified('foo')); $this->assertEmpty($entity->ble->modified('iak')); $this->assertEqual($entity->ble->modified(), array('foo' => true, 'baz' => true)); $data = array('foo' => 'bar', 'baz' => 'dib'); //it's the default data array in the test $entity = new Entity(); $entity->set($data); $entity->sync(); /* Checking empty values */ $entity->foo = ''; $this->assertTrue($entity->modified('foo')); $this->assertEqual(array('foo' => true, 'baz' => false), $entity->modified()); /* and checking null values */ $entity->sync(); $entity->foo = null; $this->assertTrue($entity->modified('foo')); }
/** * Extends the parent implementation to ensure that child documents are properly synced as well. * * @param mixed $id * @param array $data * @param array $options Options when calling this method: * - `'recursive'` _boolean_: If `true` attempts to sync nested objects as well. * Otherwise, only syncs the current object. Defaults to `true`. * @return void */ public function sync($id = null, array $data = array(), array $options = array()) { $defaults = array('recursive' => true); $options += $defaults; if (!$options['recursive']) { return parent::sync($id, $data, $options); } foreach ($this->_updated as $key => $val) { if (is_object($val) && method_exists($val, 'sync')) { $nested = isset($data[$key]) ? $data[$key] : array(); $this->_updated[$key]->sync(null, $nested, $options); } } parent::sync($id, $data, $options); }