Insert record into DB table while installing an add-on

Permalink 1 user found helpful
Hi
Sometimes it is required to insert some default record into database tables while installing an add-on. I was wondering whether it is possible or not. If possible, then could someone please tell me the code.

Thanks

ronyDdeveloper
 
shahroq replied on at Permalink Reply
shahroq
If you mean by db.xml file, you can try adding this node into the xml file:
<sql>
    <query>
        INSERT INTO `myTable` (`id`, `title`) VALUES
        (1, 'title-1'),
        (2, 'title-2');
    </query>
</sql>
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Thanks for this.. I'll have it a try, but is there any way to do that through controller.
Because if there is a lots of pre defined records that I need to insert, then I can easily create an array & pass it to save method so that it can easily inserted.

Rony
JohntheFish replied on at Permalink Reply
JohntheFish
That db.xml trick looks useful. I didn't know about that.

In a package controller, the package database tables are created by the parent::install() part. So any time after that you can use the database, or make calls to a model that interfaces to the database.

Block tables are created when the block install calls are made.

One of the tricky parts is to cope with failed installations or partially failed installations, so that the code can adapt to any partially installed data left over by a preceding failed initialisation or incomplete uninstall.

The details really depend on the nature of the tables.
shahroq replied on at Permalink Best Answer Reply
shahroq
Inserting rows into the tables at the package main controller manually should not be a problem, you just prepare sql queries and execute them.
http://www.concrete5.org/documentation/introduction/database-access...
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Thanks for your suggestion. I hope the controller coding is better than using xml.