ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Integrity.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilDBInterface;
24 
25 class Integrity
26 {
27  public function __construct(
28  private ilDBInterface $database
29  ) {
30  }
31 
50  public function check(Definition $definition): Result
51  {
52  $on = [];
53  $where = [];
54  // $definition->associations() always returns a non empty array
55  foreach ($definition->associations() as $association) {
56  $on[] = sprintf('%s = %s', $association->field()->fieldName(), $association->referenceField()->fieldName());
57  $where[] = sprintf('%s IS NULL', $association->referenceField()->fieldName());
58  foreach ($definition->ignoreValues() as $value_to_ignore) {
59  $where[] = sprintf('%s %s', $association->field()->fieldName(), $value_to_ignore);
60  }
61  }
62 
63  $result = $this->database->query(sprintf(
64  'SELECT COUNT(1) as violations FROM %s LEFT JOIN %s ON %s WHERE %s',
65  $definition->tableName(),
66  $definition->referenceTableName(),
67  implode(' AND ', $on),
68  implode(' AND ', $where),
69  ));
70 
71  $result = $this->database->fetchAssoc($result);
72 
73  return new Result((int) $result['violations']);
74  }
75 }
__construct(private ilDBInterface $database)
Definition: Integrity.php:27
check(Definition $definition)
Example: $violations = $this->check(new Definition([ new Association(new Field(&#39;mail&#39;, &#39;folder_id&#39;), new Field(&#39;mail&#39;, &#39;folder_id&#39;) ])))->violations(); Mail example: $mailId = new Field(&#39;mail&#39;, &#39;mail_id&#39;); $mailObjDataId = new Field(&#39;mail_obj_data&#39;, &#39;obj_id&#39;); $defintions = [ new Definition([new Association(new Field(&#39;mail&#39;, &#39;folder_id&#39;), $mailObjDataId)]), new Definition([new Association(new Field(&#39;mail_attachment&#39;, &#39;mail_id&#39;), $mailId)]), new Definition([new Association(new Field(&#39;mail_cron_orphaned&#39;, &#39;mail_id&#39;), $mailId)]), new Definition([new Association(new Field(&#39;mail_cron_orphaned&#39;, &#39;folder_id&#39;), $mailObjDataId)]), new Definition([new Association(new Field(&#39;mail_tree&#39;, &#39;child&#39;), $mailObjDataId)]), new Definition([new Association(new Field(&#39;mail_tree&#39;, &#39;parent&#39;), new Field(&#39;mail_tree&#39;, &#39;child&#39;, &#39;parent&#39;))], new Ignore(null, &#39;0&#39;)), ]; $results = array_map([$this, &#39;check&#39;], $defintions);.
Definition: Integrity.php:50