Build a block which can save data passed from view.php to a database table
Permalink
Hi,
I want to build a block which can save data passed from view.php to a database table. Can this be achieved by writing an action method in the controller.php? And I want to first check whether the table exists. If the table does not exist, the table should be created firstly. Otherwise, just insert the data into the table straight away. Can someone give me some examples?
Thank you very much :)
I want to build a block which can save data passed from view.php to a database table. Can this be achieved by writing an action method in the controller.php? And I want to first check whether the table exists. If the table does not exist, the table should be created firstly. Otherwise, just insert the data into the table straight away. Can someone give me some examples?
Thank you very much :)
Hi xaritas,
Thank you for your reply.
BUT what I try to do is not to create a database for the block itself. What I want to do is to create a separated database to store user input which comes from a form generated by the block, and it is not the form displayed in the block editing window.
Thanks.
Thank you for your reply.
BUT what I try to do is not to create a database for the block itself. What I want to do is to create a separated database to store user input which comes from a form generated by the block, and it is not the form displayed in the block editing window.
Thanks.
You can do all of this from within the block installation routine. For an example, check out jordanlev's manual_nav:http://www.concrete5.org/marketplace/addons/manual-nav/...
I will spoil the surprise for you, though:
Notice that it creates two tables. While the block's table, btManualNav, records whether or not the block exists, the block stores all of its data in "btManualNavLinks." You can grab this block from within any page and store whatever extra data you want. There's nothing forcing you to make a form to update the contents.
If this still doesn't work from you, like I said before, there is nothing stopping you from grabbing a handle to the database and executing code to create tables.
I will spoil the surprise for you, though:
<?xml version="1.0"?> <schema version="0.3"> <table name="btManualNav"> <field name="bID" type="I"><key /><unsigned /></field> <field name="dummy" type="C" size="10"></field> </table> <table name="btManualNavLinks"> <field name="bID" type="I"><unsigned /></field> <field name="linkToCID" type="I"><unsigned /></field> <field name="linkText" type="C" size="255"></field> <field name="position" type="I"><unsigned /></field> </table> </schema>
Notice that it creates two tables. While the block's table, btManualNav, records whether or not the block exists, the block stores all of its data in "btManualNavLinks." You can grab this block from within any page and store whatever extra data you want. There's nothing forcing you to make a form to update the contents.
If this still doesn't work from you, like I said before, there is nothing stopping you from grabbing a handle to the database and executing code to create tables.
When you create a block, you describe the schema to the ORM in a db.xml file, which acts like a migration when you install the block. There is a pretty good tutorial here:
http://www.concrete5.org/documentation/developers/blocks/understand...
If you really need an extremely dynamic schema, you might consider running a document store alongside MySQL.