multiple tables in block

Permalink 1 user found helpful
Hi, I am developing a block, and would need to insert and update data into 3 different tables. I have looked at some core blocks, and it looks like SQL statements are being used in functions within controller.php, but those functions do not seem to be used anywhere within the block files. So, how do I manually control multiple tables in a block?

 
cherrycake replied on at Permalink Reply
cherrycake
There's plenty of ways you can pull that off. Not sure how familiar you are with developing blocks but every block has it's own table defined in your db.xml file and block controller. this is the table that automatically gets added/updated to when you define form fields with identical name attributes to the table's respective field names.

If you like you say, need to add/update more than that table, you would most likely override the save($args) function in the block's controller (be sure to call the parent::__save($args) function within your own save method at some point).

In that function, the $args parameter would be the associative array with all your form fields values from your add/edit block popup. Simply pick out the value that are not saved in the block's main table and call some function in your controller or perhaps a model class that contains the sql needed to insert/update the other tables.

something like
class SomeBlockController extends BlockController {

function save($args) {
parent::__save($args);
$this->saveToAdditionalTables($args);
}

function saveToAdditionalTables($args) {
$db = Loader::db();
$db->query("INSERT INTO someBlockTable (someColumn) VALUES ($args['someColumn'])");
}

}
malthoff replied on at Permalink Reply
malthoff
Woran kann es liegen, wenn das dennoch nicht klappt. Meine SQL Query stimmt, da ich sie in phpmyadmin getestet habe und ich dort auch Werte in die Tabelle eintragen kann. Aus Concrete5 heraus kommt aber nichts in der Tabelle an. Alle anderen Blöcke funktionieren. ??
malthoff replied on at Permalink Reply
malthoff
ok i got it. My block controller wasn't syntax error free. the class ended before the save function and this error died silently. So keep in mind to double check your classes if something just happens to fail without an error.