[MODX] Создать пакет с пользовательской таблицей

Для начала нужно создать таблицу в базе данных и интегрировать ее в MODx.

  1. Установить пакет CMPGenerator.
  2. Создать таблицу с желаемой структурой через phpMyAdmin (Поле с автоинкремент долно называться id)
    Пример моей таблицы:
    CREATE TABLE IF NOT EXISTS `modx_go_feedback` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `name` varchar(255) NOT NULL,
      `email` varchar(255) NOT NULL,
      `message` text NOT NULL,
      `data` text NOT NULL,
      `custom1` text NOT NULL,
      `custom2` text NOT NULL,
      `custom3` text NOT NULL,
      `flag` int(11) NOT NULL,
      `phone` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
        
  3. В CMPGenerator нажать Create Package

  4. Создаем сниппет для тестирования:
    $output = '';// this is what the snippet will return
    // add package so xpdo can be used:
    $package_path = $modx->getOption('core_path').'components/gofeedback/model/'; //Вместо "gofeedback" написать то, что указывали в "Package Name"
    // see the scheme file and the xml model element and you will see the attribute package and that must match here
    
    $modx->addPackage('gofeedback', $package_path);  //Вместо "gofeedback" написать то, что указывали в "Package Name"
    
    // lets add some data!
    // see the scheme file and the xml object element and you will see the attribute class and that must match here
    // the class name is taken from table names without the prefixed, and is capitalized.
    $myRow = $modx->newObject('GoFeedback'); // class из файла схемы (core/components/[gofeedback]/model/[gofeedback]/[gofeedback].mysql.schema.xml)
     
    $data = array(
      'name' => 'MODX Revolution',
      'message' => 'A great CMS product...'
    );
    $myRow->fromArray($data);
     
    if ( !$myRow->save() ) {
      $output .= '<p>Could not create row</p>';
    } else {
      $output .= '<p>Created row successfully</p>';
    }
    // now lets show the data in a quick and dirty table:
    $output .= '
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Description</th>
        </tr>';
    $query = $modx->newQuery('GoFeedback');
    $rows = $modx->getIterator('GoFeedback', $query);
     
    /* iterate */
    $list = array();
    foreach ($rows as $row) {
        // from object to array you can also do $row->get('name');
        $row_array = $row->toArray();
     
        $output .= '
        <tr>
            <td>'.$row_array['id'].'</td>
            <td>'.$row_array['name'].'</td>
            <td>'.$row_array['message'].'</td>
        </tr>';
    }
    $output .= '
    </table>';
     
    return $output;

Далее будет создан пакет для транспортировки, и страница в админке, где можно будет простматривать данные.

Заметки и ссылки по теме:

  • https://ilyaut.ru/xpdo/
  • https://bezumkin.ru/sections/components/252/
  • https://ilyaut.ru/tips-and-tricks/create-a-build-of-modx-revo/

Комментарии (0)

  1. Напишите первый комментарий
*Комментарий будет опубликован после проверки модератором

Комментарии easyComm

tooladmin 17 марта 2018, 22:33

https://rtfm.modx.com/xpdo/2.x/getting-started/creating-a-model-with-xpdo/defining-a-schema/

Похожие статьи

Наш сайт использует куки, нажмите «ОК» если вы не против
OK