Inserting Tagify Values into a Database

Intro

You can insert the value of the Tagify component into a database. In our example we will show you how to store it in a json field.

Database Setup

We have a products table and each product can be assigned to different categories. We’d like to use the Tagify component to pick the categories per each product, when inserting it in the database.
Our products database table is quite simple, an id field, name, description and category:

Our Category field type is json, and it will be used to store the categories selected in the Tagify input as an array:

Tagify Setup

We’ve created an insert record server action and form, using this database table. Now let’s setup Tagify for the category input.

Data source

We created a server action, returning our categories (or tags) from a database. It’s quite a simple server action, containing just a database query:

Let’s add this on the page, as we will need it as a source for the Tagify input:
Click Add New Component:

Under Data, select Server Connect:

And click the select server action button:

Select the server action returning the categories from the database:

And you are done.

Tagify Options

Now as we have our Data Source set up, lets add Tagify on the page. Select the Category text input and open the Convert to menu:

Then select Tagify:

Under the Tagify properties, select your data source:

This is the database query returned by the server action:

Then select the text to be displayed for the options:

Select the database field storing the categories names:

Then do the same for the Value field:

Select the database field storing the categories IDs:

Scroll down the options and enable the No Custom option. This way we disable entering custom/non-existing tags, so the user can only select from the existing ones:

And we are done. Let’s insert a new product and add some categories to it:

You can see the categories IDs stored in the category json field in our database table:

Having the array of category IDs in the database, you can now use them to filter the categories table and display the categories on the product details page.

4 Likes

I personally hate comma separated list in database fields and prefer to do something like this based on a many to many relationship.
Can i use the data in the category POST variable as the basis for a repeat to separate out each “tag” value into a separate table??

It looks like it is just sent to the server as a comma separated list, so you should be able to use split() on the posted value to make it a repeatable array

The tutorial seems to skip the POST variables and does not actually show the final insert API action , just the result but looking at the screenshot

image

It appears it is passed as an object

That’s not a comma separated list, but an array, stored in a json db field.

Yes you can repeat through it using a repeat step directly :slight_smile:

1 Like

As Teodor says, it is an array. Treat the same as any other multiple value input:
image

It’s really slick! Thanks @Teodor and the team…

Ben, Sorry, you seem to miss the main purpose of my post. For me a list by any other name is still a list whether it is a list, an array, json or otherwise resulting in an output like 1,2,3 etc
I never put and form of lists into database fields, i consider it contrary to the rules of relational databases (i know many do but not me)
Therefore the question was can i iterate with a repeat over the value returned which i am please to see is a yes…

1 Like

If it comes through as an array, just use a repeat pointing to the $_POST value and use an insert inside the repeat to add a record to a table for each value.

1 Like

We will be bringing more power next week to relational databases and specially when working with junction tables to realize one-to-many or many-to-many relations.

So you can easily store multi values in junction tables automatically!

9 Likes

Thank you, like music to my ears

2 Likes

This is an update I’ve been waiting for! Huge thanks Wappler team.

2 Likes

I want what Brian is having. Thanks team.

2 Likes

@Teodor it would be possible would the list come in a selection format without the client having to start typing? where we could select more than one option just like in select2?

maybe in the mix format but it’s not working, what am i doing wrong?

Do you want to show a drop-down with the values?

Here it’s described how to use it as a select:

That’s right, but I can’t choose more than one option?

same as select2

Ok then if I understand you correctly, just switch to default mode and disable user input. This will show a drop-down and allow multiple selection.

1 Like

very very very cool :slight_smile:

Just to add if you want to retain the search function you can set the Min Chars to 0 and leave User input as allowed, (also check No Custom to stop the user creating new options). This makes the drop down appear without needing to type, but if you do type it filters the options as you go.

2 Likes

A post was split to a new topic: Problems inserting data in the database