Create table in database

Hellow, i'm developing a plugin that requires a new table in database . What advice can you offer me to develop this table?

  • I can't give you any advice regarding creating the new table itself (I never have done it). But my question would be: are you really sure that you need a new table? Maybe the plugin can also be implemented (maybe it's even better that way) without creating a new table.

  • The situation is as follows. When a user is adding a new file, it will be able to select a file to match the name to relate to the file that it is adding the time.

    I do not know if I was clear.

  • Where can we start from, mmmh, Oh! Creating  elgg tables is a peace of cake and a glass of coffee or tea or a glass of water! In the same file tree  start.php of your plugin, create a activate.php and a folder/file  called   sql/create_tables.sql

    In your new create_tables.sql put this info inside:


    --CREATE TABLE IF NOT EXISTS `elgg_user_file_details` (


    CREATE TABLE IF NOT EXISTS `elgg_user_file_details` (
      `file_details_id` int(11) NOT NULL auto_increment,
      `relate_file_id` int(11) NOT NULL,
      `user_file_id` int(12) NOT NULL,
      `user_id` int(12) NOT NULL,
      `date` datetime NOT NULL,
      `status` int(5) NOT NULL,
      `total_files` float(10,2) NOT NULL,
      PRIMARY KEY  (`file_related_id`)


    Then in activate.php file copy and paste this info:


    * File related Table creator

    // create tables if not exist
    $prefix = elgg_get_config('dbprefix');
    $tables = get_db_tables();
    if (! in_array("{$prefix}elgg_user_file_details", $tables)) {
        run_sql_script(__DIR__ . '/sql/create_tables.sql');
        system_message("Table created: {$prefix}elgg_user_file_details");


    Now here is what you have just done. When you click activate button on admin section, the table elgg_user_file_details will be created automatically.

    Just as iionly has suggested most of your new tables can be haddled smoothly by using Elgg Metadata. And this is the power of Elgg engine.

  • You do not need to create a new table for this.  Elgg has a relationship model for linking files to one another for example.

  • add_entity_relationship($file1->guid, 'my_relationship', $file2->guid);

    remove_entity_relationship($file1->guid, 'my_relationship', $file2->guid);

    $relationship_exists = check_entity_relationship($file1->guid, 'my_relationship', $file2->guid);


    // get related files

    $files = elgg_get_entities_from_relationship(array(

        'type' => 'object',

        'subtype' => 'file',

        'relationship' => 'my_relationship',

        'relationship_guid' => $file1->guid


Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking