Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckReferenceColumnHasIndex.php
Go to the documentation of this file.
1 <?php
7 
12 
20 {
24  const ERROR_TYPE = 'reference_column_without_unique_index';
25 
29  const ERROR_MESSAGE = 'Reference column %s in reference table %s do not have index';
30 
34  public function validate(Schema $schema)
35  {
36  $errors = [];
37  foreach ($schema->getTables() as $table) {
38  foreach ($table->getReferenceConstraints() as $constraint) {
39  $referenceColumnName = $constraint->getReferenceColumn()->getName();
40  $indexesAndConstraints = array_merge(
41  $constraint->getReferenceTable()->getConstraints(),
42  $constraint->getReferenceTable()->getIndexes()
43  );
44  foreach ($indexesAndConstraints as $key) {
45  if ($key instanceof Internal || $key instanceof Index) {
46  if (in_array($referenceColumnName, $key->getColumnNames())) {
47  continue 2;
48  }
49  }
50  }
51 
52  $errors[] = [
53  'column' => $referenceColumnName,
54  'message' => sprintf(
55  self::ERROR_MESSAGE,
56  $referenceColumnName,
57  $constraint->getReferenceTable()->getName()
58  )
59  ];
60  }
61  }
62 
63  return $errors;
64  }
65 }
$table
Definition: trigger.php:14
$errors
Definition: overview.phtml:9