How to read and write into the database (with a page)

How to read and write into the database (with a page) 1.0

No permission to download
First: Thanks for this great resource. I learned a lot from it.

Same. What's the probem? Error?

For me, it did not work out of the box either, throwing a security error. This brought me to the right track pretty fast: The security token is missing from the submit form code.

Add
PHP:
<input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
to the form in the page template, and it works just fine, at least for me it did.

// Afterthought: I'm still on 1.1.2, but don't think that's the cause for my issue. I may be wrong on that, of course.
 
Got all the way to the end with little trouble. However, when hitting Send Message, it says "Route simpletext/write could not be found." Something change in the new version of Xenforo that might be causing it? I've gone so far as to to copy and paste the tutorial just to be sure there were no typos.
 
Got all the way to the end with little trouble. However, when hitting Send Message, it says "Route simpletext/write could not be found." Something change in the new version of Xenforo that might be causing it? I've gone so far as to to copy and paste the tutorial just to be sure there were no typos.
Same problem here too, running xf 1.2.3.

Has anyone managed to find a solution?
 
Same problem here too, running xf 1.2.3.

Has anyone managed to find a solution?
This is embarrassing, but, my problem was due to the fact that I had the add-on disabled (while testing other stuff). Enabling the add-on again got it working as advertised.
 
Thanks for creating this tutorial. It's helpful, but Step 5 lost me.
  • What is the purpose of defining all of the fields and their type in _getFields()?
  • What is _getUpdateCondition($tableName) and what does it actually do? What do you mean by getting the "SQL condition"?
  • Why is _getSimpleTextModel() useful to have in the DataWriter?
And, finally, how would you actually go about writing a new entry to the xf_simple_text table?
 
Thanks for creating this tutorial. It's helpful, but Step 5 lost me.
  • What is the purpose of defining all of the fields and their type in _getFields()?
  • What is _getUpdateCondition($tableName) and what does it actually do? What do you mean by getting the "SQL condition"?
  • Why is _getSimpleTextModel() useful to have in the DataWriter?
And, finally, how would you actually go about writing a new entry to the xf_simple_text table?

1 - The _getFields() function get the fields that are defined for the table. It returns an array with each key being a table name and a further array with each key being a field in database.

2 - The _getUpdateCondition gets a sql condition to update the existing record. Let's say you want to edit an entry in the xf_simple_text table. You will create a new DataWriter and set the existing data using the data you got from the model. After that, you want to change the text of this entry. How does XenForo will now which record to update? With this function you give to XenForo a condition (example: where simple_id = 1), so this way XenForo knows which record in that table you are trying to update.

3 - The _getSimpleTextModel: This is just a function that returns the model we will use. This is just useful if you use this model more then once in your file. Let's say you have 4 actions inside a controller. Inside each action you get the SimpleText model 15 times. Will you write the same thing each time inside each action? The better way to do this is to create an function that performs what you want to do. Later, if you want to change anything, just change the function.
Finally - If you want to create a new entry to the table, use this code:

PHP:
//Create a instance of our DataWriter
$dwSimpleText = XenForo_DataWriter::create('SimpleText_DataWriter_SimpleText');

//Set the field with the data we filtered
$dwSimpleText->set('simple_text', 'YOUR TEXT HERE');

//Save in the database
$dwSimpleText->save();
 
This is embarrassing, but, my problem was due to the fact that I had the add-on disabled (while testing other stuff). Enabling the add-on again got it working as advertised.
Yeah, somehow I got mine working again, too. Then I had to step away from it a bit. Can't quite remember why it started working, but as I recall, I had something configured elsewhere wrong.
 
Quick question,

I am looking to build a serious add-on with lots of database reading and writing. Would it be smart to go ahead and create a new database entirely for this xf add-on or would it be smart to use the xf db created by xenforo?

If creating another DB is a better option, would I have any permission difficulties fetching forum statistics and data from the default database to be used with the new database? (I would only need 'read only' perms to read from the default database into the new database with this idea I have)

Lastly, but not least. This tutorial is very well written, but if one is relatively new with coding an application for XF from scratch. What is the normal process flow of how a developer STARTS with his or her idea and ENDS with the idea?

For example:

1. Get my Idea
2. Write it down on paper
3. Organize the idea into flow charts
4. Simplify program & add-on even more
5. Polish up flow chart(s)
6. Build & Code Application


I ask because, one of the more serious problems I have been encountering is trying to figure out all the fields I might needs to accommodate this add-on.

What if I miss or want to add a field later? Is this an easy option and task to add after the database is set up initially?


Thanks for this awesome tutorial and assistance.
 
Last edited:
Thanks for this resource. With that said, this is a crap ton of code for a simple database write and read. I guess this the way intended by xenforo but then again it has turned what should have only required 5 lines of code into dozens of lines of needless code.
 
Thanks for this resource. With that said, this is a crap ton of code for a simple database write and read. I guess this the way intended by xenforo but then again it has turned what should have only required 5 lines of code into dozens of lines of needless code.


Its intended to teach you add-on development. Not how to one line read and one line write to the database. Chances are you already know that. Half of the addons out there dont need install code but ones that have custom tables do so for example that is taught here.
 
Last edited:
On step 10 you have to add
PHP:
<input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />

Otherwise you get a security error.
IDK if there's another work around but I just searched the error and that's the line that seems to be suggested to solve the security error.

Great tutorial :)

Same as well though placing it above the rest of the code didn't work, I placed it in <form> below the <input>
 
Back
Top Bottom