Search code examples
phpdatabasepluginsilias

Call to a member function tableColumnExists() on null


I'm making a plugin for ILIAS and I am gtting this error when I try to call for anything database related. I'm including the dbupdate file in the config file to be able to update it and change things while developping the plugin.

(This is because I believe that ILIAS only uses the dbupdate when installing a plugin.)

The code it gives an error on:

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

Column and 'id' do exist in the database. I believe this needs to work to be able to save data to the database in the future.

Update

This is the code that 'calls' :

<?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());
            }
        }

    }
    ?>

And this is the called file:

<?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
        )
    );
}
?>

Solution

  • I solved this by making another class named ilconfig. it contains this very important part:

    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;
        }
    

    I hope this helps other people trying to develop plugins in ILIAS!