This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi,
I wanted to edit a simple model with many-to-many relationships and data in the intermediate table (pivot). Here is a simplified example of what I tried to edit:
With the Relation widget, it is not possible to add pivot data, so I created a widget myself https://github.com/myst6re/october-plugin-complex-relation It adds edit forms on each possible relation (so I can have a date field for each existing region).
But it is more complicated than I expected, and there some limitations. For example I cannot add file upload inputs.
So questions :
- What is the best solution to edit pivot data easily?
- How can I support file upload with my widget?
hi, i think it is perfectly possible to edit pivot data form natively with OctoberCMS. did you have a look at this demo plugin : https://github.com/daftspunk/oc-test-plugin
Thanks for the link. I started playing with it, and I reproduced the bugs I found recently.
For example if you go to the "create user" page and you want to create a role in pivot data and pivot model tabs, it doesn't work (and the error message is weird). In fact it only works with the "add role" button. I opened a ticket recently about that
Secondly, daftspunk put relation with pivot data and pivot model in "create user" page, but you can't save the association because the user doesn't exist yet. So you must put a relation editor with pivot data only in the edit page. (I think it is a known problem anyway)
And finally, there is no file attachment field in pivot relation in this plugin, but I'm sure it will not work. For that, I have maybe a solution by using the "mediafinder" type instead of "fileupload".
Edit: nope it doesn't work with "mediafinder", the attachement is not created in system_files table. And with "fileupload", the parent model is not referenced in the created attachment (yes it means that one entry is created, by not used, that's a dead entry).
Last updated
ok thanks i see what you mean now. in the meantime, do you know a way to make this pivot fields dynamic using addField or filterField methods ? i cant figure out a way to make this work in a pivot model.
I don't understand the question: which addField/filterField methods?
I think there is two ways to add a pivot association in the create page:
- With the relation behavior, you must create a temporary model when the association creation is requested. But I don't know how you can delete this model if the page is not saved...
- Or create the association at the time the page is saved, when the model is created. I found a way to do this in Laravel (doc here in Inserting Related Models (Many To Many) > Attaching Many To Many Models) and that what I use in my plugin. I'm sure it is possible to improve my plugin to render like the relation behavior.
Last updated
i will explain my case. my app is managing companies data. A company will belong to many Associations. in this relation, i need a pivot model with different fields. there are differents fields attached to the relation between a company and a professional association (NGO for eample). exemple: sometimes there is an expiry date and a scope of work. sometimes there is nothing, sometimes there is a membership number and an expiry date. It is completely dynamic and based on a the type of the association. So i m trying to implement this behavior on the backend, but without success.
one idea would be to provide all the possible fields available trough all the type of associations and hide the unecessary one based on the type of associations. -> using the method filterField provided by the Model Class. another idea would be to add dynamicc the necessary field based on the type of association -> using the extendFormField (from Controller class) and addField (from WidgetForm class).
this 2 methods work well with a simple model but not with the pivot model apparently.
also im thinking i would need a fileupload in the pivot model as well.
so right now, my solution is to transform all my "belongToMany" relation into a "hasMany" relation and here the filterField method is working.
Regarding your point : "With the relation behavior, you must create a temporary model when the association creation is requested. But I don't know how you can delete this model if the page is not saved..." you can save your model in the backend and then attach the relation ?
how can i test your plugin ? i mean what are the options available ? i will give it a try
So you need to inherit your pivot table, either using single table inheritance (ugly on the db), or polymorphic relationships (better). I don't know if Laravel know how to deal with that, I'll search that way.
For my plugin, it works like the relation form widget, but you need to specify two extra things:
- The list of fields to edit in the pivot table
- The list of required fields, used to create or not the relation if the specified fields are filled (I know this thing is ugly, that because I removed the checkboxes to select or not the association)
Basic example:
fields:
regions:
label: "Regions"
type: complex_relation
emptyOption: "No regions"
required_fields:
- date
form:
fields:
date:
label: "Date"
type: datepicker
mode: date
minDate: '1970-01-01'
website:
label: "Website"
You may have noticed that I've not written "pivot[date]" and "pivot[website]", that because I need to write more code to make it works :p
1-8 of 8