How to handle save function for block when there are more tables?


1. I am new to concrete5 as i have checked with the block part i saw that in add/edit part when we save it, it passes the post variable to save function thats fine for the block that have the single table but what for the table that have more then one table how will the save for other table? Do we have to send some parameter that it calls the related save function or do we have to follow some naming convention?

2. Also i am not getting that in the blocks in admin if i want to edit the general setting for some block then where can i code for it and how can i access it?

View Replies: View Best Answer
jordanlev replied on at Permalink Reply
1) If you have more than 1 table, you need to handle saving the data yourself in the save() function. For example:
public function save($args) {
   parent::save($args); //Call this first to save the primary record
   //Now save the other record using SQL
   $db = Loader::db();
   $query = 'INSERT INTO btMyBlockOtherTableName (bID, field1, field2) VALUES (?, ?, ?)';
   $params = array($this->bID, $args['field1'], $args['field2']); //Use the "parameterized query" style to prevent SQL injection errors
   $db->Execute($query, $params);

You can also use the builit-in ActiveRecord library to save your data without SQL statements, but it's a bit more advanced, and probably not worth the trouble unless you block is fairly complicated.

2) Not sure what you mean exactly by this -- do you mean the default settings for a new block when it's added to a page?
savan replied on at Permalink Reply
Hi jordanlev,

Thanks for your reply.

1. for the 2 tables i mean say for example i have one form in my block that is displayed when the block is added to any page now if i submit that form and i want to save that data then how can i call the function from the controller?

2.I mean how can we work with the dashboard if i want to ask the details from admin then how can that be done?
jordanlev replied on at Permalink Best Answer Reply
Okay I think I understand now.
You should download and look at the code for my Email List Signup block:

It has code that saves form data and also shows a dashboard page.

But here's the general idea:
1) In your block's view.php file, output a form with this for the action:
<?php echo $this->action('my_function_name'); ?>

Then in the block's controller.php file, have a function that is the name you passed into the form action but with "action_" before it:
public function action_my_function_name() {
   //Form data available via $this->post():
   $description = $this->post('description');
   //Do whatever you need to save data (probably SQL queries)
   //When you're done, you probably want to redirect the user:
   global $c;
   $redirect = Loader::helper('navigation')->getCollectionURL($c);
   header("Location: " . $redirect);

2) Create a single_page (and add code to install it from your package controller), and in the single_page you can have a form and code to save settings to the database, or if they are fairly basic settings you can use the built-in config library:

Hope that helps!

savan replied on at Permalink Reply
Thanks for your help it helped me perfectly as i need thank you very much

I have one more query if you can give me some idea

- in concrete5 as i see all the thing mainly works on the bases of block and packages and each block have the view that displays the main content of it. In here what if i want to create a huge part like i want a cart then how it will be managable as for each page how can i give the view and how can i give the code for save cart and all

i haven't got much big blocks or package to see so i am not clear with it so if you can give me some idea it will be much helpful for me
jordanlev replied on at Permalink Reply
savan replied on at Permalink Reply
Hi Jordan,

Thanks a lot for your reply. Your reply helped me a lot thank you very much

jordanlev replied on at Permalink Reply
You're welcome. If you don't mind marking that as the accepted answer, it would help people in the future find it more easily (and give me some karma as well).