My goals:
- Follow the normalization of the database.
- Ability to track changes in my tables.
- The ability to restore the state of my database from a given point in time, for example. last month.
- Separate the data processing code so that the input can come from either an HTML form or a script in another language (Ruby / perl).
For this, I chose a database design similar to that described in this answer:
stack overflow
However, when the user updates several fields, the same metadata must be inserted into several tables containing the same columns, and my code becomes duplicate.
Example:
The user submits data via an HTML form. PHP processes the data as shown below using Propel ORM.
function insertEvent($name, $venueId, $user, $date, $id = 0) {
$event = new Event();
$event->setName($name);
$event->setVenueId($venueId);
$event->setUser($user);
$event->setValidFrom($date);
$event->setValidUntil(null);
$event->save();
}
function insertEventPhonenumber($phonenumber, $pid, $user, $date, $id = 0) {
$event_phonenumber = new EventPhonenumber();
$event_phonenumber->setPid($pid);
$event_phonenumber->setPhonenumber($phonenumber);
$event_phonenumber->setUser($user);
$event_phonenumber->setValidFrom($date);
$event_phonenumber->setValidUntil(null);
$event_phonenumber->save();
}
function insertEventArtistId($artistId, $pid, $user, $date, $id = 0) {
$event_artistId = new EventArtistId();
$event_artistId->setPid($pid);
$event_artistId->setArtistId($artistId);
$event_artistId->setUser($user);
$event_artistId->setValidFrom($date);
$event_artistId->setValidUntil(null);
$event_artistId->save();
}
My problem:
There are more tables in my complete code than the three in this example.
Marked with // 1, // 2 and // 3, you see that data entry will often be identical.
In my stomach I don’t like it. I tried search engines with queries such as “common columns in SQL inserts of queries across multiple tables” and wording options, without finding anything directly related to my problem.
Is this bad practice really like me?
How can I minimize repetition in my code?