How to reduce duplicate code when pasting the same metadata into the same columns across multiple tables?

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) {
    //validation and formatting..
    //if id == 0, new event, 
    //else, this is an update, archive the old database row on successful processing and maintain a common id as a parent
    //...
    $event = new Event();
    $event->setName($name); 
    $event->setVenueId($venueId); 
    $event->setUser($user); //1
    $event->setValidFrom($date); //1
    $event->setValidUntil(null); //1 
    $event->save(); 
    // ...
}

function insertEventPhonenumber($phonenumber, $pid, $user, $date, $id = 0) {
    //...
    $event_phonenumber = new EventPhonenumber();
    $event_phonenumber->setPid($pid); //2
    $event_phonenumber->setPhonenumber($phonenumber);
    $event_phonenumber->setUser($user); //2
    $event_phonenumber->setValidFrom($date); //2
    $event_phonenumber->setValidUntil(null); //2 
    $event_phonenumber->save(); 
    // ...
} 

function insertEventArtistId($artistId, $pid, $user, $date, $id = 0) {
    //...
    $event_artistId = new EventArtistId();
    $event_artistId->setPid($pid); //3
    $event_artistId->setArtistId($artistId);
    $event_artistId->setUser($user); //3
    $event_artistId->setValidFrom($date); //3
    $event_artistId->setValidUntil(null); //3
    $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?

+4
source share
1 answer

? , call_user_func .

<?php
class MyApp {


     static function insert($name, $params) {
         $ob = new $name();
         foreach($params as $k=>$v) {
            $op = array($ob,"set".ucfirst($k));
            //if (is_callable($op)) {
                call_user_func($op,$v);
            //}
         }
         $ob->save();
     } 

}

MyApp::insert(
    "EventArtistId",
    array(
        "pid"=>$_REQUEST['pid'],
        "artistId"=>$_REQUEST['artistId'],
        "user" => $_REQUEST['user'],
        "validFrom" => $_REQUEST['date'], // may need to convert date
        "validUntil" => isset($_REQUEST['validUntil']) ? $_REQUEST['validUntil'] : null,
    )
);

// More cool
// MyApp::insert($_REQUEST['action'],$_REQUEST['params']);

?>
+1

All Articles