Search code examples
phpmysqldrupaldrupal-7migration

Check in PHP if MySQL Column in Table exists


I want to migrate the content of a Drupal7 website to another Drupal7 system using the migrate-module.

I have to add and map fields. The fields of every content type in drupal are stored in a table called field_revision_field_name. The value of the most fields are in the column field_name_value. But some fields have another structure, so I want to check if the field_name_value column exists.

I am joining tables and adding Fields in a mysql query in a loop. The problem is, that not every table "field_revision_".$typeFields[$i] (alias is $typeFields[$i]."_table") has a column $typeFields[$i]."_value":

for ($i=0; $i < sizeof($typeFields); $i++) {
    $query->join(
        "field_revision_".$typeFields[$i], 
        $typeFields[$i]."_table", 
        "n.nid = ".$typeFields[$i]."_table.entity_id"
    );
    $query->addField($typeFields[$i]."_table", $typeFields[$i]."_value");       
}

I want to check this before I do the query, something like this:

for ($i=0; $i < sizeof($typeFields); $i++) {
  if($typeFields[$i]."_table" has Column $typeFields[$i]."_value"){
    $query->join(
        "field_revision_".$typeFields[$i], 
        $typeFields[$i]."_table", 
        "n.nid = ".$typeFields[$i]."_table.entity_id"
    );
    $query->addField($typeFields[$i]."_table", $typeFields[$i]."_value");
  }       
}

Solution

  • It realy was something like this XY Problem.

    The D7 function field_info_field() has information about the columns where the data is stored, so i dont have to ask if the columns exists.

    But thankyou