Beispiel #1
0
 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);
 }
Beispiel #2
0
 /**
  * Deletes a Model from the database
  * @param Model $model The Model that was deleted
  */
 public function delete(Model $model)
 {
     if (!$model->isNew()) {
         $stmtResult = new StatementResult();
         list($condition, $conditionParameters) = $this->createAndCondition($model->getInitial());
         $result = $this->database->delete($this->relation, $condition, $conditionParameters, $stmtResult);
         if ($stmtResult->getAffectedRecords() === 1) {
             $this->syncModel($model, $result[0]);
         } else {
             throw new \LogicException("The delete operation did not return exactly one record! " . "The Model must have been changed or deleted by another operation!");
         }
     } else {
         throw new \LogicException("Unable to delete an unsaved model!");
     }
 }