【问题标题】:How do I create a content type from SQL query?如何从 SQL 查询创建内容类型?
【发布时间】:2018-07-24 19:43:34
【问题描述】:
<?php

function customtable_permission() {
    return array(
        'show people' => array(
            'title' => t('List of people'),
            'description' => t('The table'),
        ),
    );

}

function customtable_menu() {

    $items = array();

    $items['people'] = array(
        'type' => MENU_NORMAL_ITEM,
        'title' => t('Title'),
        'description' => 'This page should show a table from a remote DB',
        'page callback' => 'customtable_db_data',
        'access arguments' => array('show people'),

    );

    return $items;
}

function customtable_db_data() {

    db_set_active('remote_database');

    $results = db_query("SELECT * FROM {people}");

    $header = array(t('Id'), t('Name'), t('Department'), t('Division'), t('Insurance'));

    $rows = array();

    foreach($results AS $result) {

        $rows[] = array(
            $result->id,
            $result->name,
            $result->department,
            $result->division,
            $result->insurance,
        );

    }
    db_set_active('default');

    return theme('table', array('header'=> $header, 'rows' => $rows));
}

?>

这一切都很好,我可以访问 site.com/people 并查看数据库中的所有条目都打印在表格中

但我想要可以过滤每一列的文本框。用户可以按名称或特定的保险或部门进行搜索。我认为以编程方式是可能的,但我想知道是否有更“drupal”的方法。内容类型能够过滤其字段。我是否要根据我的查询创建内容类型?我不完全知道。任何帮助表示赞赏。

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    我认为最好的方法是将查询结果迁移到 drupal 内容类型,为此您需要使用 migrate api。

    -安装并启用 migrate 和 migrate_ui drupal 模块。 - 使用您的字段创建您想要的任何内容类型。使用 drupal 界面。 - 使用 migrate api 创建一个自定义模块。例如:

    /sites/all/modules/custom/migrate_customtable/migrate_customtable.migrate.inc

    function migrate_customtable_migrate_api() {
    
        $api = array(
            'api' => 2,
            'groups' => array(
                'custom_table' => array(
                    'title' => t('Custom Table'),
                ),
            ),
            'migrations' => array(
                'Projects' => array(
                    'class_name' => 'CustomTableMigration',
                    'group_name' => 'custom_table',
                    'event_log' => ''
                ),
    
            ),
        );
    
        return $api;
    
    
    }
    

    然后,创建一个名为 CustomTableMigration.inc 的类,它将包含迁移:

    <?php
    
    /**
     * Created by PhpStorm.
     * User: ldcontreras
     * Date: 25/07/18
     * Time: 10:13
     */
    class CustomTableMigration extends Migration {
    
    
        public function __construct($arguments) {
            parent::__construct($arguments);
            $query = Database::getConnection('default', 'migrate_custom_table')//this must be define in your settins.php file
            ->select('people')
                ->fields('productos_new', array(
                        'id',
                        'name',
                        'department',
                        'division',
                        'insurance',
                    )
                );
    
    
            $this->source = new MigrateSourceSQL($query, array(), NULL, array(map_joinable => FALSE));
            $this->destination = new MigrateDestinationNode('content_type_machine_name'); //the content type created
    
            $this->map = new MigrateSQLMap($this->machineName,
                array(
                    'id' => array(
                        'type' => 'int',
                        'unsigned' => TRUE,
                        'not null' => TRUE,
                        'description' => t('Source ID'),
                    )
                ),
                MigrateDestinationNode::getKeySchema()
            );
    
            $this->addFieldMapping('title', 'name');
            $this->addFieldMapping('field_department_content_type', 'department');
            $this->addFieldMapping('field_division_content_type', 'division');
            $this->addFieldMapping('field_insurance_content_type', 'insurance');
    
    
            $this->addUnmigratedDestinations(array(
                'body:format',
                'comment',
                'is_new',
                'log',
                'promote',
                'revision',
                'revision_uid',
                'tnid',
                'totalcount',
                'daycount',
                'timestamp',
                'path',
                'translate',
                'sticky',
                'uid',
            ));
    
        }
    
    
    }
    

    最后,启用您的自定义模块并使用 drush 运行迁移。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多