php - 在 null 上调用成员函数 tableColumnExists()

标签 php database plugins ilias

我正在为 ILIAS 制作一个插件,当我尝试调用任何与数据库相关的内容时,我遇到了这个错误。我将 dbupdate 文件包含在配置文件中,以便能够在开发插件时更新它并进行更改。

(这是因为我相信 ILIAS 只在安装插件时使用 dbupdate。)

它给出错误的代码:

if(!$ilDB->tableColumnExists('rep_robj_xptg_data','id'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'id',
        array(
            'type'           => 'integer',
            'length'         => 4000
        )
    );
}

数据库中确实存在列和“id”。我相信这需要能够在将来将数据保存到数据库中。

更新

这是“调用”的代码:

<?php

    include ("/Customizing/global/plugins/Services/Repository/RepositoryObject/Presentations2Go/sql/dbupdate.php");
    require_once('./Services/Component/classes/class.ilPluginConfigGUI.php');
    require_once('class.ilPresentations2GoPlugin.php');
    require_once('class.ilObjPresentations2Go.php');
    //require ("/Customizing/global/plugins/Services/Repository/RepositoryObject/Presentations2Go/sql/dbupdate.php");

    /**
     * Presentations2Go configuration user interface class
     *
     * @author Bonny van den Bovenkamp
     * @version $Id$
     *
     */
    class ilPresentations2GoConfigGUI extends ilPluginConfigGUI
    {
        /**
        * Handles all commmands, default is "configure"
        */
        function performCommand($cmd)
        {

            switch ($cmd)
            {
                case "configure":
                case "save":
                    $this->$cmd();
                    break;

            }
        }

        /**
         * Configure screen
         */
        function configure()
        {
            global $tpl;

            $form = $this->initConfigurationForm();
            $tpl->setContent($form->getHTML());
        }

        //
        // From here on, this is just an Presentations2Go implementation using
        // a standard form (without saving anything)
        //

        /**
         * Init configuration form.
         *
         * @return object form object
         */
        public function initConfigurationForm()
        {
            global $lng, $ilCtrl;

            $pl = $this->getPluginObject();

            include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
            $form = new ilPropertyFormGUI();

            // setting 1 (a checkbox)
            $cb = new ilCheckboxInputGUI($pl->txt("setting_1"), "setting_1");
            $form->addItem($cb);

            // setting 2 (text)
            $ti = new ilTextInputGUI($pl->txt("setting_2"), "setting_2");
            $ti->setRequired(true);
            $ti->setMaxLength(10);
            $ti->setSize(10);
            $form->addItem($ti);

            $form->addCommandButton("save", $lng->txt("save"));

            $form->setTitle($pl->txt("Presentations2Go configuration"));
            $form->setFormAction($ilCtrl->getFormAction($this));

            return $form;
        }

        /**
         * Save form input (currently does not save anything to db)
         *
         */
        public function save()
        {
            global $tpl, $lng, $ilCtrl;

            $pl = $this->getPluginObject();

            $form = $this->initConfigurationForm();
            if ($form->checkInput())
            {
                $set1 = $form->getInput("setting_1");
                $set2 = $form->getInput("setting_2");


                ilUtil::sendSuccess($pl->txt("saving_invoked"), true);
                $ilCtrl->redirect($this, "configure");
            }
            else
            {
                $form->setValuesByPost();
                $tpl->setContent($form->getHtml());
            }
        }

    }
    ?>

这是被调用的文件:

<?php


if(!$ilDB->tableColumnExists('rep_robj_xptg_data','id'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'id',
        array(
            'type'           => 'integer',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','is_online'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'is_online',
        array(
            'type'           => 'integer',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_one'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_one',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_two'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_two',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
if(!$ilDB->tableColumnExists('rep_robj_xptg_data','option_three'))
{
    $ilDB->addTableColumn(
        'rep_robj_xptg_data',
        'option_three',
        array(
            'type'           => 'text',
            'length'         => 4
        )
    );
}
?>

最佳答案

我通过创建另一个名为 ilconfig 的类解决了这个问题。它包含这个非常重要的部分:

public function initDB() {
        global $ilDB;
        if (!$ilDB->tableExists($this->getTableName())) {
            $fields = array(
                'config_key' => array(
                    'type' => 'text',
                    'length' => 128,
                    'notnull' => true
                ),
                'config_value' => array(
                    'type' => 'clob',
                    'notnull' => false
                ),
            );
            $ilDB->createTable($this->getTableName(), $fields);
            $ilDB->addPrimaryKey($this->getTableName(), array( "config_key" ));
        }

        return true;
    }

我希望这能帮助其他尝试在 ILIAS 中开发插件的人!

关于php - 在 null 上调用成员函数 tableColumnExists(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30461061/

相关文章:

php - mysql查询获取文章的评论总数

javascript - 如何使用 html5 和 javascript 访问/插入服务器数据库

android - 无法创建 Android SQLite 数据库 : PRAGMA error

javascript - 从插件的辅助功能访问 jQuery 对象

javascript - 在 Chrome 中检测插件的版本号

javascript - 使用 angularjs 和 php 登录

php - 从数据库读取数据出错

php - 生成随机GUID时未初始化的字符串偏移量通知

database - 如何测试代码生成工具?

java - 有什么好的 hive 插件教程吗?