56 if ($this->databaseExist) {
57 $existingTables = $this->db->getTablesName();
59 $tables = Reflexion::getAllJoinTables($this->models);
60 if (isset($this->metadatas)) {
61 foreach ($this->metadatas as $model => $metas) {
62 $tablename = $metas[
'#tableName'];
63 if (\array_search($tablename, $existingTables) ===
false && \array_search($tablename, $tables) ===
false) {
64 $tables[$model] = $tablename;
68 return \array_diff($tables, $existingTables);
81 $this->databaseExist =
true;
82 if (! $this->checkDatabase()) {
83 $result[
'database'] = $this->dbOffset;
85 $result[
'nonExistingTables'] = $this->_getNonExistingTables();
86 foreach ($this->models as $model) {
87 $metadatas = $this->metadatas[$model];
88 $tableName = $metadatas[
'#tableName'];
89 $updatedPks = $this->checkPrimaryKeys($model);
90 if (\count($updatedPks) > 0) {
91 $result[
'pks'][$tableName] = $updatedPks;
93 $updatedFields = $this->getUpdatedFields($model);
94 if (\count($updatedFields) > 0) {
95 $result[
'updatedFields'][$tableName] = $updatedFields;
97 $manyToOneUpdateds = $this->checkManyToOne($model);
98 if (\count($manyToOneUpdateds) > 0) {
99 $result[
'manyToOne'][$tableName] = $manyToOneUpdateds;
101 $manyToManyUpdateds = $this->checkManyToMany($model);
102 if (\count($manyToManyUpdateds) > 0) {
103 $result[
'manyToMany'][$tableName] = $manyToManyUpdateds;
106 return $this->checkResults = $result;
143 $metadatas = $this->metadatas[$model];
144 $tableName = $metadatas[
'#tableName'];
145 if ($this->tableExists($tableName)) {
146 $fields = $metadatas[
'#fieldNames'];
147 $fieldTypes = $metadatas[
'#fieldTypes'];
148 $nullables = $metadatas[
'#nullable'];
149 $notSerializable = $metadatas[
'#notSerializable'];
150 $originalFieldInfos = [];
151 if ($this->databaseExist) {
152 $originalFieldInfos = $this->db->getFieldsInfos($tableName);
154 foreach ($fields as $member => $field) {
155 if (\array_search($member, $notSerializable) ===
false) {
156 $nullable = \array_search($member, $nullables) !==
false;
158 'table' => $tableName,
161 'type' => $fieldTypes[$member],
162 'extra' => $nullable ?
'' :
'NOT NULL'
165 if (! isset($originalFieldInfos[$field])) {
166 $result[
'missing'][$model][] = $fieldInfos;
167 } elseif ($fieldTypes[$member] !==
'mixed' && ($fieldTypes[$member] !== $originalFieldInfos[$field][
'Type']) || ($originalFieldInfos[$field][
'Nullable'] !==
'NO' && ! $nullable)) {
168 $result[
'updated'][$model][] = $fieldInfos;
209 $metadatas = $this->metadatas[$model];
210 $manyToOnes = $metadatas[
'#manyToOne'] ?? [];
211 $joinColumns = $metadatas[
'#joinColumn'] ?? [];
212 $table = $metadatas[
'#tableName'];
214 if ($this->tableExists($table)) {
215 foreach ($manyToOnes as $manyToOneMember) {
216 $joinColumn = $joinColumns[$manyToOneMember];
217 $fkClass = $joinColumn[
'className'];
218 $fkField = $joinColumn[
'name'];
219 $fkTable = $this->metadatas[$fkClass][
'#tableName'];
220 $fkId = $this->metadatas[$fkClass][
'#primaryKeys'][0] ??
'id';
221 $result = \array_merge($result, $this->checkFk($table, $fkField, $fkTable, $fkId));
227 private function checkFk($table, $fkField, $fkTable, $fkId) {
230 if ($this->databaseExist && $this->tableExists($table)) {
231 $originalFks = $this->db->getForeignKeys($fkTable, $fkId, $this->db->getDbName());
234 foreach ($originalFks as $ofk) {
235 if ($ofk[
'TABLE_NAME'] === $table && $ofk[
'COLUMN_NAME'] === $fkField) {
243 'column' => $fkField,
244 'fkTable' => $fkTable,
252 $metadatas = $this->metadatas[$model];
253 $manyToManys = $metadatas[
'#manyToMany'] ?? [];
254 $joinTables = $metadatas[
'#joinTable'] ?? [];
255 $table = $metadatas[
'#tableName'];
257 if ($this->tableExists($table)) {
258 foreach ($manyToManys as $member => $manyToManyInfos) {
259 $joinTableInfos = $joinTables[$member];
260 $joinTableName = $joinTableInfos[
'name'];
261 $targetEntity = $manyToManyInfos[
'targetEntity'];
262 $fkTable = $this->metadatas[$targetEntity][
'#tableName'];
263 $fkId = $this->metadatas[$targetEntity][
'#primaryKeys'][0] ??
'id';
264 $fkId = $joinTableInfos[
'inverseJoinColumns'][
'referencedColumnName'] ?? $fkId;
265 $fkField = $joinTableInfos[
'inverseJoinColumns'][
'name'] ?? ($fkId . \ucfirst($fkTable));
266 $result = \array_merge($result, $this->checkFk($joinTableName, $fkField, $fkTable, $fkId));
281 $dbResults = $this->checkResults;
283 if (isset($dbResults[
'database'])) {
284 $displayCallable(
'error',
'database',
"The database at offset <b>" . $dbResults[
'database'] .
"</b> does not exist!");
286 if (\count($notExistingTables = $dbResults[
'nonExistingTables']) > 0) {
287 $notExistingTables = \array_unique($notExistingTables);
288 foreach ($notExistingTables as $model => $table) {
289 if (\is_string($model)) {
290 $displayCallable(
'warning',
'Missing table',
"The table <b>" . $table .
"</b> does not exist for the model <b>" . $model .
"</b>.");
292 $displayCallable(
'warning',
'Missing table',
"The table <b>" . $table .
"</b> does not exist.");
296 if (\count($uFields = $this->getResultUpdatedFields()) > 0) {
297 foreach ($uFields as $table => $updatedFieldInfos) {
298 if (isset($updatedFieldInfos[
'missing'])) {
299 $model = \array_key_first($updatedFieldInfos[
'missing']);
300 if (\count($fInfos = $updatedFieldInfos[
'missing'][$model] ?? []) > 0) {
301 $names = $this->concatArrayKeyValue($fInfos,
function ($value) {
302 return $value[
'name'];
304 $displayCallable(
'warning',
'Missing columns',
"Missing fields in table <b>`$table`</b> for the model <b>`$model`</b>: <b>($names)</b>");
307 if (isset($updatedFieldInfos[
'updated'])) {
308 $model = \array_key_first($updatedFieldInfos[
'updated']);
309 if (\count($fInfos = $updatedFieldInfos[
'updated'][$model] ?? []) > 0) {
310 $names = $this->concatArrayKeyValue($fInfos,
function ($value) {
311 return $value[
'name'];
313 $displayCallable(
'warning',
'Updated columns',
"Updated fields in table <b>`$table`</b> for the model <b>`$model`</b>: <b>($names)</b>");
318 if (\count($pks = $this->getResultPrimaryKeys()) > 0) {
319 foreach ($pks as $table => $pksFieldInfos) {
320 $model = $pksFieldInfos[
'model'];
321 $names = implode(
',', $pksFieldInfos[
'primaryKeys']);
322 $displayCallable(
'warning',
'Missing key',
"Missing primary keys in table <b>`$table`</b> for the model <b>`$model`</b>: <b>($names)</b>");
325 if (\count($manyToOnes = $this->getResultManyToOne()) > 0) {
326 foreach ($manyToOnes as $table => $manyToOneFieldInfos) {
327 $names = $this->concatArrayKeyValue($manyToOneFieldInfos,
function ($value) {
328 return $value[
'table'] .
'.' . $value[
'column'] .
' => ' . $value[
'fkTable'] .
'.' . $value[
'fkId'];
330 $displayCallable(
'warning',
'Missing hashtag (manyToOne)',
"Missing foreign keys in table <b>`$table`</b> : <b>($names)</b>");
334 if (\count($manyToManys = $this->getResultManyToMany()) > 0) {
335 foreach ($manyToManys as $table => $manyToManyFieldInfos) {
336 $names = $this->concatArrayKeyValue($manyToManyFieldInfos,
function ($value) {
337 return $value[
'table'] .
'.' . $value[
'column'] .
' => ' . $value[
'fkTable'] .
'.' . $value[
'fkId'];
339 $displayCallable(
'warning',
'Missing hashtag (manyToMany)',
"Missing foreign keys for manyToMany with table <b>`$table`</b> : <b>($names)</b>");