Dave Bixler Mon Feb 04 21:24:58 -0500 2013

Subject: save to update or insert

My application currently has about 20 models. Until now, the system has only ever had to do inserts and not updates. I'm trying to change that now to add new functionality. The current functionality loads the model's data, and then does a save on the current object. Example:

$this->curObj->field1 = blah;
$this->curObj->field2 = blah;
....
$this->curObj->save ();

Is it possible to, before the save() method, insert something that would be similar to:

$this->curObj->field1 = foo;
$this->curObj->field2 = bar;
$existingId = $this->curObj->findObject ();
if ($existingId != null) {
$this->curObj->id = $existingId;
}
$this->curObj->save ();

The goal being to set the ID of the current object to be saved to get activerecord to see the current object, not as a new instance, but as an update?

Each class extends a base class, so each one could provide its own customized "findObject()" implementation.

Thanks in advance.


Antoine Sledge Thu Feb 07 10:53:35 -0500 2013

You should possibly rename findObject() to findObjectId() (It confused me that it wasn't an object =p), and potentially just have findObject return an instance of the object that is found in the db.

I do not remember the exact syntax, but then you can just replace the "found object's" attributes with the new object's freshly changed attributes, and then save the "found object".

(1-1/1)