Welcome Guest, Not a member yet? Register   Sign In
Saving multiple relationships to same table: Which method is preferred?
#1

[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
            )
        );
}
#2

[eluser]Tominator[/eluser]
Can't you make an array?

SQL: WHERE name IN ('John', 'Sally', 'Steve', 'DaMan');

PHP: mysql_fetch_array() -> $query[0] => creator, $query[1] => editor ...

I really don't know, if this is good solution, but it has just one query to DB Smile That's why I think it's faster and cleaner. It's up to you, if it's good to choose Tongue

Note: I'm not using DMZ class (I'm using Active record), so I wrote it as basic PHP, SQL - to make it easy to understand.

Tom.
#3

[eluser]Genki1[/eluser]
@Tom: Thanks for the reply. My question is specific to DMZ so the PHP/SQL solution is not what I'm asking for.
#4

[eluser]Tominator[/eluser]
I wrote it in PHP/SQL because I don't know DMZ, but it must contain these functions (but it has another calling).
#5

[eluser]Genki1[/eluser]
Understood and thanks, but my question is not HOW to do the task but, rather, using DMZ functions WHICH approach is preferable. I'd like to hear from someone experienced in DMZ.




Theme © iAndrew 2016 - Forum software by © MyBB