How to extend any MODX table

This is translation of russian topic.

Once again it was necessary to change the site table, which can not be changed.

What do people usually do in such cases? As a usual, they edit a kernel or the necessary component, and lose the possibility of their updating. But this is wrong, and you can solve the problem differently.

We write the usual plugin, mark the OnMODXInit event for it and change the model for the required classes of the system or add-ons. For example, I added the manager id to the miniShop2 order:
<?php
switch ($modx->event->name) {

	case 'OnMODXInit':
		$modx->loadClass('msOrder');
		$modx->map['msOrder']['fields']['manager_id'] = 0;
		$modx->map['msOrder']['fieldMeta']['manager_id'] = array(
			'dbtype' => 'int',
			'precision' => 10,
			'attributes' => 'unsigned',
			'phptype' => 'integer',
			'null' => true,
			'default' => 0,
		);
		break;
}
It remains only to physically add your columns to the database tables, and MODX works with them, as with the native ones. Saves, selects, filters — everything is as it should be.

No more problems with updates.

This way you can extend any table on the fly. And if you suddenly do not need it anymore — just turn off the plug-in.
Василий Наумкин
27 августа 2017, 01:54
modx.pro
1
1 668
+1

Комментарии: 0

Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
0