public static function EntityUpdateTestHelper::checkFieldList in Entity Update 8
Same name and namespace in other branches
- 2.0.x tests/modules/entity_update_tests/src/EntityUpdateTestHelper.php \Drupal\entity_update_tests\EntityUpdateTestHelper::checkFieldList()
Check a database table fields list and match with privided list.
Return value
bool|array TRUE if has correct fields list. FALSE if exception. Array if different.
4 calls to EntityUpdateTestHelper::checkFieldList()
- EntityUpdateFunctionsTest::testEntityUpdateAll in modules/
entity_update_tests/ src/ Tests/ EntityUpdateFunctionsTest.php - Entity update function : all.
- EntityUpdateFunctionsTest::testEntityUpdateBasic in modules/
entity_update_tests/ src/ Tests/ EntityUpdateFunctionsTest.php - Entity update function : basic.
- EntityUpdateFunctionsTest::testEntityUpdateClean in modules/
entity_update_tests/ src/ Tests/ EntityUpdateFunctionsTest.php - Entity update function : clean.
- EntityUpdateFunctionsTest::testEntityUpdateSel in modules/
entity_update_tests/ src/ Tests/ EntityUpdateFunctionsTest.php - Update a selected entity type.
File
- modules/
entity_update_tests/ src/ EntityUpdateTestHelper.php, line 76
Class
- EntityUpdateTestHelper
- EntityUpdateTest Helper functions.
Namespace
Drupal\entity_update_testsCode
public static function checkFieldList($table, array $fields) {
// Combine fields.
$fields_must = array_combine($fields, $fields);
// Current database fields list.
$fields_curr = [];
// Get Database connection.
$con = Database::getConnection();
// Add table prefix.
if ($table_prefix = $con
->tablePrefix()) {
$table = $table_prefix . $table;
}
try {
$fields_list = $con
->query("DESCRIBE `{$table}`")
->fetchAll();
foreach ($fields_list as $field) {
$fields_curr[$field->Field] = $field->Field;
}
// Check differents.
$array_diff = array_diff($fields_must, $fields_curr);
if (empty($array_diff)) {
$array_diff = array_diff($fields_curr, $fields_must);
// Tables matched.
if (empty($array_diff)) {
return TRUE;
}
}
// Two tables are different.
return $array_diff;
} catch (\Exception $ex) {
return FALSE;
}
return FALSE;
}