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.
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.
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 :/
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.
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.
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.
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" ?
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
You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.