Adding a database table for a block

Adding a database table for a block

by Shish Moom -
Number of replies: 4
I'm developing a block which needs a table to store data in. I RTFM with regards to block development, but it didn't mention databases at all...

Back when I worked on a module a year or so back I remember it being possible to include a .sql file which would be run when the module was first noticed, to create any tables, or update them if the format changed with a new module version -- how can I do the same thing for a block?
Average of ratings: -
In reply to Shish Moom

Re: Adding a database table for a block

by David Hicks -
Damn... good point. You could add code in the init() function that checked to see if your database table existed, and if not created it (maybe include a "version" field in that database, too). This does mean that every time your block is viewed it's doing another database access or two and a couple of conditional tests - potentially affecting performance?

--
David Hicks
In reply to Shish Moom

Re: Adding a database table for a block

by Jan Dierckx -

Adding database tables to a block is done in a similar way as to a module. Just add a .sql file (or a php file if you are updating) to a subdirectory db inside your block directory. It will be executed when you visit the admin page after installing the block.

A nice example is the rss_client block which stores links to newsfeeds inside a separate table.

In reply to Jan Dierckx

Re: Adding a database table for a block

by David Hicks -
Woops - scratch my previous post, then! I did wonder about that, but couldn't find any documentation or remember seeing any examples. I can add a bit into the "blocks" section of the documentation wiki, explain for future reference.

--
David Hicks
In reply to Jan Dierckx

Re: Adding a database table for a block

by Shish Moom -
Thanks, that looks like it does everything I need~