public function testsaveOnlyIfDirty() { $dirty_allergy_name = 'test allergy modified'; // make sure this attribute is not in our DB Allergy::model()->deleteAllByAttributes($this->testattributes); Allergy::model()->deleteAllByAttributes(array('name' => $dirty_allergy_name)); /* New Model **/ $allergy = new Allergy(); $allergy->setAttributes($this->testattributes); // test to save a new model if (!$allergy->saveOnlyIfDirty()->save()) { $this->fail('Saving new model fails on saveOnlyIfDirty()'); } //check if the record really saved in the DB $allergy = Allergy::model()->findByAttributes($this->testattributes); $this->assertNotNull($allergy); $this->assertEquals($this->testattributes['name'], $allergy->name); /* Clean Model **/ $allergyAttributes = $allergy->getAttributes(); //Perform a save without modifing the attributes if (!$allergy->saveOnlyIfDirty()->save()) { $this->fail('Updating model fails on saveOnlyIfDirty()'); } // compare attributes before and after, as no database action was required //we except the attributes are the same (e.g.: last_modified_date) // Retrive the model again to see what is it in the DB $allergy = Allergy::model()->findByAttributes($this->testattributes); $this->assertEquals($allergyAttributes['id'], $allergy->id); $this->assertEquals($allergyAttributes['name'], $allergy->name); $this->assertEquals($allergyAttributes['last_modified_user_id'], $allergy->last_modified_user_id); $this->assertEquals($allergyAttributes['last_modified_date'], $allergy->last_modified_date); $this->assertEquals($allergyAttributes['created_user_id'], $allergy->created_user_id); $this->assertEquals($allergyAttributes['created_date'], $allergy->created_date); $this->assertEquals($allergyAttributes['active'], $allergy->active); $this->assertEquals($allergyAttributes['display_order'], $allergy->display_order); /* Dirty Model **/ //let's modify the model $allergy->name = $dirty_allergy_name; $allergy->last_modified_date = date('Y-m-d H:i:s'); if (!$allergy->saveOnlyIfDirty()->save()) { $this->fail('Updating modified model fails on saveOnlyIfDirty()'); } $allergy = Allergy::model()->findByAttributes(array('name' => $dirty_allergy_name)); $this->assertNotNull($allergy); $this->assertEquals($allergyAttributes['id'], $allergy->id); $this->assertEquals($dirty_allergy_name, $allergy->name); Allergy::model()->deleteAllByAttributes(array('name' => $dirty_allergy_name)); Allergy::model()->deleteAllByAttributes($this->testattributes); }