title: PHP w/ PDO: Create variable number of class fields on the fly and execute them in a single transaction
body: <p>I'll do my best to describe the problem in details so you can provide a good answer and reward you by accepting it.</p>
  
  <p>My class BaseEntry and its BaseEntryMapper are responsible for managing the basic details of a business directory entry (let's stick just to "phone, fax, cell").</p>
  
  <p>Classes BaseEntryMeta and BaseEntryMetaMapper should deal with any additional inputs from a user (phone_2, phone_3, ..., fax_2, fax_3, ..., cell_2, .... but also many other features!) he may or may not make.</p>
  
  <p>While the baseEntry (+Mapper) class work fine and save OK, I'm having issues making the other one to work. That is due to the fact I cannot know how many additional features a user may leave. I know I can create those on the fly so I intend to use the names of my form's input fields to set them with a loop like so:</p>
  
  <pre><code>$baseEntryMeta-&gt;$formFieldName = $_POST['formFieldName']
  </code></pre>
  
  <p>For the basic part, I call the saveBaseEntry method like this (note how an object is being passed):</p>
  
  <pre><code>$baseEntry = new BaseEntry();
  $baseEntryMapper = new BaseEntryMapper($pdo);
  $baseEntryMapper-&gt;saveBaseEntry($baseEntry);
  </code></pre>
  
  <p>Now the other class (additional inputs / features of an entry). I tried passing an object too, but it won't work. I decided to collect all the info into an array and pass that array instead of an object (I adjusted the method to expect an array):</p>
  
  <pre><code>$metaData = array(
      array(
          $baseEntryMeta-&gt;meta_key = "phone_2",
          $baseEntryMeta-&gt;meta_value = "999 555 2222",
      ),
      array(
          $baseEntryMeta-&gt;meta_key = "phone_3",
          $baseEntryMeta-&gt;meta_value = "999 555 6666",
      ),
      array(
          $baseEntryMeta-&gt;meta_key = "fax_2",
          $baseEntryMeta-&gt;meta_value = "999 555 2222",
      ),
      array(
          $baseEntryMeta-&gt;meta_key = "cell_2",
          $baseEntryMeta-&gt;meta_value = "999 444 3333",
      )
  );    
  
  $baseEntryMeta = new BaseEntryMeta();
  $baseEntryMetaMapper = new BaseEntryMetaMapper($pdo);
  
  $baseEntryMetaMapper-&gt;saveBaseEntryMeta($metaData);
  </code></pre>
  
  <p>Now my saveBaseEntryMeta() method:</p>
  
  <pre><code>public function saveEntryMeta(array $data)
  {
      $sql = "INSERT INTO ".$this-&gt;table." (entry_id, meta_key, meta_value) 
          VALUES (:entry_id, :meta_key, :meta_value)
      ";
      $stmt = $this-&gt;db-&gt;prepare($sql);
  
      $stmt-&gt;bindParam("entry_id", $entryMeta-&gt;entry_id);
      // the above param wasn't mentioned, but it is needed in the code
      $stmt-&gt;bindParam("meta_key", $entryMeta-&gt;meta_key);
      $stmt-&gt;bindParam("meta_value", $entryMeta-&gt;meta_value);
      $stmt-&gt;execute($data);
  }
  </code></pre>
  
  <p>Some questions:
  1. Is the array that is passed structured in a way that is acceptable by the PDO?
  2. I think I read somewhere this part (after VALUES):</p>
  
  <p>(:entry_id, :meta_key, :meta_value)</p>
  
  <p>should be duplicated for each array element (through a loop).
  3. If the above is correct, what should I pass to the execute statement so that info is saved as separate rows?
  4. Could all saving be done in a single transaction?</p>
  
  <p>Thank you!</p>
  
  <p>See my other questions related to this project (I found some solutions myself but I'm willing to hear new suggestions and accept good answers) - they may provide more details on issues I'm having on the project.</p>
  
  <p>EDIT: I moved my previous edit(s) into a separate answer so i can later accept it for anyone else who might have the same problem in the future.</p>