Add new table in existing block

Add new table in existing block

by Mohammad Nur Un Nabi -
Number of replies: 3

Hi,

I have created one block name 'details'. There are some table in this block which are created in plugin time. Now I need to add one more table name 'block_details_test'. So create a file db->update.php and scripts are given bellow...

?php

function xmldb_block_cmanager_upgrade($oldversion) {
global $CFG, $DB;

$dbman = $DB->get_manager();

$result = true;

//$newrec = new stdClass();
//$newrec->varname = 'selfcat';
//$newrec->value = 'no';

//$DB->insert_record('block_cmanager_config', $newrec, false);


//alter database for required/optional fields

// Conditionally launch rename field timesent

$table = new xmldb_table('block_details_test');
$field = new xmldb_field('reqfield', XMLDB_TYPE_INTEGER, '10', null, null, null, null);

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}


upgrade_block_savepoint($result, 22013093002, 'details');

 

return $result;

}
?>

but when I update the plugin changing the version, it does not create any table and not showing any error. Any one help me to find out where is the problem

Average of ratings: -
In reply to Mohammad Nur Un Nabi

Re: Add new table in existing block

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

A few points about this file.

Firstly, as with every plugin included in Moodle core, the file is called 'db/upgrade.php' (not update).

Secondly, the upgrade code should be wrapped in 'if ($oldversion < 2013093002) {' otherwise the code will run on every version number change (I assume the version number is as I've put, not as you've put in the upgrade_block_savepoint - we're still in 2013, not 22013!).

Thirdly - do not put closing PHP tags at the end of files '?>' - this leaves you open to really hard to find bugs if you accidentally put some whitespace after the closing tag (and PHP automatically assumes a closing tag if it doesn't find one at the end of the file).

Finally, you should always put 'defined('MOODLE_INTERNAL') || die();' as the first line after the opening PHP tag, to make sure that no one can accidentally visit your library file and execute code.

In reply to Davo Smith

Re: Add new table in existing block

by Mohammad Nur Un Nabi -

Hi Davon,

Thanks for your reply. I have re-written the file and changed the name upgrade.php. But still facing the problem. 

Block Name: block_tushar

Previous Version: 2013093001

upgrade.php is given bellow:

<?php

function xmldb_block_tushar_upgrade($oldversion) {
global $CFG;

$result = TRUE;
if ($oldversion < 2013093002) {

// Define table fees to be created.
$table = new xmldb_table('blcok_tushar_fees');

// Adding fields to table fees.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('studentiid', XMLDB_TYPE_CHAR, '256', null, XMLDB_NOTNULL, null, null);
$table->add_field('totalfees', XMLDB_TYPE_NUMBER, '20, 2', null, XMLDB_NOTNULL, null, null);
$table->add_field('initialpayment', XMLDB_TYPE_NUMBER, '15, 2', null, null, null, null);

// Adding keys to table fees.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for fees.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}


// Tuition savepoint reached.
upgrade_block_savepoint(true, 2013093002, 'block_tushar');
}


return $result;

}

It will great if you find out where is the problem.  when I click on the upgrade button..it shows "Ugrading to new version" and in middle the block name "block_tushar". There is no error message and successful message 

In reply to Mohammad Nur Un Nabi

Re: Add new table in existing block

by Mohammad Nur Un Nabi -

Hi Davon,

Problem solved. It's now working. Thanks again for your reply

Scripts that work:

<?php

function xmldb_block_tushar_upgrade($oldversion=0) {
global $DB;
$dbman = $DB->get_manager();

$result = TRUE;
if ($oldversion < 2013100916) {

// Define table fees to be created.
$table = new xmldb_table('blcok_tushar_fees');

// Adding fields to table fees.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('studentiid', XMLDB_TYPE_CHAR, '256', null, XMLDB_NOTNULL, null, null);
$table->add_field('totalfees', XMLDB_TYPE_NUMBER, '20, 2', null, XMLDB_NOTNULL, null, null);
$table->add_field('initialpayment', XMLDB_TYPE_NUMBER, '15, 2', null, null, null, null);

// Adding keys to table fees.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for fees.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}


// Tuition savepoint reached.
upgrade_block_savepoint(true, 2013100916, 'tushar');
}


return $result;

}

Average of ratings: Useful (1)