如何通过SQL查询创建内容类型?

时间:2018-07-24 19:43:34

标签: drupal drupal-7

<?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,在表中很好地打印数据库中的所有条目

但是我想要可以过滤每一列的文本框。用户可以按名称或特定的保险或部门进行搜索。我认为可以通过编程实现,但我想知道是否存在更“愚蠢”的方法。内容类型可以过滤其字段。我是否可以根据查询创建内容类型?我不知道感谢您的协助。

1 个答案:

答案 0 :(得分:1)

我认为做到这一点的最佳方法是将查询结果迁移到drupal内容类型,为此,您需要使用migration api。

-安装并启用了migration和migration_ui drupal模块。 -使用drupal界面创建所需的任何内容类型。 -使用迁移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运行迁移。

相关问题