lithium\data\Entity::sync PHP Метод

sync() публичный Метод

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().
См. также: lithium\data\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 sync($id = null, array $data = array(), array $options = array())
    {
        $defaults = array('materialize' => true, 'dematerialize' => false);
        $options += $defaults;
        $model = $this->_model;
        $key = array();
        if ($options['materialize']) {
            $this->_exists = true;
        }
        if ($options['dematerialize']) {
            $this->_exists = false;
        }
        if ($id && $model) {
            $key = $model::meta('key');
            $key = is_array($key) ? array_combine($key, $id) : array($key => $id);
        }
        $this->_increment = array();
        $this->_data = $this->_updated = $key + $data + $this->_updated;
    }

Usage Example

Пример #1
0
 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');
 }
All Usage Examples Of lithium\data\Entity::sync