That’s rather disingenuous and you’ve inserted a few steps there that you normally wouldn’t recognise
To do an update without the update method you need to:
- Read
id
(or other pk) from the from
1a. Read additional keys from the form for composite keys
- Call isset (multiple times for composite keys)
- Read the return value
- Evalulate the if statement
- Run the else condition
- Call the update method
- Call the query method
- Execute the update query
(It’s easy to bulk it out like you did )
So more realistically it’s 8.5 vs 10 steps for updating.
Let’s compare inserting. For inserting using your approach you need to:
- Read
id
(or other pk) from the from
1a. Read additional keys from the form for composite keys
- Call isset (multiple times for composite keys)
- Read the return value
- Evaluate the if statement
- Run the else condition
- Call the insert method
- Call the query method
- Execute the insert query
Whereas using save
you need to:
- Call save method
- Call insert method
- Call query method
- Execute insert query
That’s 4 vs 8 steps. Assuming you have a ratio of 1:1 inserts to updates you’d execute a lot less code using save
. In reality a lot of tables see far more inserts than updates (logs, user registration details, shopping carts, etc).
And that’s for each form. Even by your own rather skewed numbers by the time you have reused the save
method in four different forms you’ve saved work.
Again, you’re missing the main point: DRY. You don’t need to write the same logic for every single form you create.
Really, apples to apples you need to:
- Determine whether you are doing an insert or update
- Call either insert or update
The only difference between the approaches is that using save
, step 1 is done in the database rather than the PHP code.
I think this is more of a philosophical issue and this kind of stack counting is meaningless. After all if it mattered, we would not use functions or objects at all and everything would be at the root level of a .php file.
When the form is submitted, do you really care whether you are dealing with a new or existing record? What you want to do is store the information in a database.
If you used a serialized array to store your data you’d do this:
$records[$data['id']] = $data;
file_put_contents($file, json_encode($records));
You don’t care if you’re overwriting an existing record or inserting a new one there. Why is a database any different?
Thanks, I’m glad you have found it useful