[eluser]Genki1[/eluser]
Using the example in the DMZ User Guide, the table "post" has multiple fields related to the table "user". [See: "Advanced Relationships"]
In the example function below, which option, "A" or "B", would be preferable for saving changes to the multiple fields related to the 'user' table?
As shown below, I'm going to update four fields in the "post" table: creator, editor, reviewer and approver. Is it faster/better/cleaner to use Option A or Option B (or even something else)?
Option "A": create only ONE user object "$u" and execute save() MULTIPLE times.
Option "B": create MULTIPLE user objects "$a, $b, $c, $d" and execute save() only ONCE.
Here's the code -- which option is preferred?
Code:
function update_post()
{
$p = new Post();
# Locate the record to be updated
$p->where('title', 'DMZ Test')->get();
# Update various fields
# Option A: create ONE object and execute MULTIPLE save(), like this:
# Instantiate user object
$u = new User();
# Locate each user record and save
$u->where('name', 'John')->get();
$p->save_creator($u);
$u->where('name', 'Sally')->get();
$p->save_editor($u);
$u->where('name', 'Steve')->get();
$p->save_reviewer($u);
$u->where('name', 'DaMan')->get();
$p->save_approver($u);
# Option B: create MULTIPLE objects and do ONE save(), like this:
# Instantiate objects
$a = new User();
$b = new User();
$c = new User();
$d = new User();
# Locate user records
$a->where('name', 'John')->get();
$b->where('name', 'Sally')->get();
$c->where('name', 'Steve')->get();
$d->where('name', 'DaMan')->get();
# Save
$sr->save(array(
'creator' => $a,
'editor' => $b,
'reviewer' => $c,
'approver' => $d
)
);
}