Troiscent
Troiscent

Hello,

I'm using the builder plugin to create my own custom content type. This content type have a lot of options. Instead of creating a database column for each options, I would like to create a single option column and save all my options as a json to that column.

Is it possible ? If yes, how can I do that ?

Thanks

Alex

Tschallacka
Tschallacka

Yes.

In your database have a field of type text or longtext(depending on how big you think it'll get) For our example sake let's name this field awesome_object

In your model you add this:

public class AwesomeModel extends Model {
   //... stuff
   public $jsonable = ['awesome_object'];
   // ... more stuff
}

Then in your fields.yaml you do

fields:
     awesome_object[name]:
         label: name
     awesome_object[awesome_number]:
         label: awesome number.

And that should work :-)

Last updated

Troiscent
Troiscent

Thanks for your reply.

Awesome ! So simple, so elegant. Each day, october cms impress me a little more.

Troiscent
Troiscent

If you are sill around here, I have another question.

Now I have my awesome_object with some options, how can I display each options as a separate columns in columns.yaml ?

I tried lot of different things, without success.

Thanks

Eoler
Eoler

Troiscent said: Now I have my awesome_object with some options, how can I display each options as a separate columns in columns.yaml ?

I would probably write a generic custom column type that extracts and displays data from JSONable fields depending on list column configuration parameters: https://octobercms.com/docs/backend/lists#custom-column-types

Troiscent
Troiscent

Thanks for the answer Eoler.

There is no "simpler" solution ? Cause if I can correctly deal with json stuff in database, it will be the base of a lot of plugins to manage all the options of different content types.

I saw there is a "select" option in columns.yaml that allow to make custom sql select statement, but I don't find any syntax that would allow me to select awesome_object[name] (tried awesome_object.name, awesome_object[name], awesome_object->name) everything failed :/

Eoler
Eoler

Troiscent said: There is no "simpler" solution ? Cause if I can correctly deal with json stuff in database, it will be the base of a lot of plugins to manage all the options of different content types.

Nothing simpler comes to mind at the moment. But you write it once and use in every project afterwards...

I saw there is a "select" option in columns.yaml that allow to make custom sql select statement, but I don't find any syntax that would allow me to select awesome_object[name] (tried awesome_object.name, awesome_object[name], awesome_object->name) everything failed :/

I don't think select could work here, there's no nested query built for model with JSOnable fields.

Troiscent
Troiscent

Hello,

I played with custom columns type this weekend, but it doesn't work, and I don't think it can work actually without modification to the core of october CMS.

The main problem is that each column must have a unique key in the columns.yaml file. That unique key need to be the name of the column in the database.

So if I write something like this :

awesome_object
    label: Name
    type: json
    jsonFrom: name
awesome_object
    label: Address
    type: json
    jsonFrom: address

Even with my custom json column type, it doesn't work because october can't deal with 2 column with the same 'awesome_object' name.

if I do something like this :

awesome_object[name]
    label: Name
    type: json
    jsonFrom: name
awesome_object[address]
    label: Address
    type: json
    jsonFrom: address

It doesn't work too because October CMS don't know any 'awesome_object[name]' column in the database, so the $value parameter that is passed to my custom column type function is null.

It would be great that when you define a column type with brackets in the key, (example awesome_object[name]) october would understand that I want to output the name property of my awesome_object column in the databse.

Should not be a big deal to change, a regex well placed that would recognize brackets. There is already that logic with fields.yaml that should be extended to columns.yaml to keep things elegant simple and intuitive (which is 3 important keywords in the october cms speech)

Edit : In addition to that, I would like to add that I would like to keep the "type" available for json object, for example, I would have an "awesome_object[published]" as a boolean, and want to display it as a "Switch" type in my column.

That's why I think working on the column name is better as it still alow the user to apply a type to the json property you try to display.

Last updated

alxy
alxy

Yeah, every columns needs a unique name, but it doesn't mean that column needs to exist in the database. Just name them awesome_object_name for example and make sure they are neither searchable nor sortable. You can theoretically try something like object_name__property_name and use __ as delimiter to separate the column name (containing the json) and the property.

Troiscent
Troiscent

Hello alxy, thank you for your answer.

If my column don't have the name of my database column, the $value parameter is = null on my custom column type function. How can I specity to october to get the value of "awesome_object" with a column name of "awesome_object_name" ?

alxy
alxy

Hi, as you can read in the docs you habe access to $record (which is the model instance) as well. Just use this to access the value like $record->awesome_object['property'].

1-11 of 11