public function testSanity() { $m = new Model(); $this->assertTrue($m->isNew()); $m2 = new Model(['firstname' => 'Johny', 'lastname' => 'Bravo', 'age' => 40, 'salary' => 500]); $this->assertFalse($m2->isNew()); $m2->setValue('firstname', 'Peter'); $this->assertFalse($m2->isNew()); $this->assertEquals('Peter', $m2->getValue('firstname')); $this->assertEquals('Bravo', $m2->getValue('lastname')); $updates = array_keys($m2->getUpdates()); $diff = array_diff(['firstname'], $updates); $this->assertCount(0, $diff); $m2->setValue('age', 42); $updates = array_keys($m2->getUpdates()); $diff = array_diff(['firstname', 'age'], $updates); $this->assertCount(0, $diff); $data = array_values($m2->getData()); $diff = array_diff(['Peter', 'Bravo', 42, 500], $data); $this->assertCount(0, $diff); $initial = array_values($m2->getInitial()); $diff = array_diff(['Johny', 'Bravo', 40, 500], $initial); $this->assertCount(0, $diff); }
/** * Inserts a new Model into the database and updates the Model with * the data return from the database * @param Model $model * @throws InvalidConfigException */ protected function insertModel(Model $model) { $stmtResult = new StatementResult(); $result = $this->database->insert($this->relation, $this->convertFromModel($model->getData()), $stmtResult); if ($stmtResult->getAffectedRecords() === 1) { $this->syncModel($model, $result[0]); } else { throw new \LogicException("The insert operation did not return exactly one record!"); } }