This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I'm using a widget to allow users to submit an event and choose from their assigned locations which one they want to host at including the option for a preferred. I've tried a lot of different options, and I can't seem to get the values to change before the Event data is saved. I am trying to modify the data for a belongsToMany() relation with pivot table data. I simplified everything down to the base items and only the important parts. I need help getting this to save properly. I've tried hidden input fields to allow 0 entries already, but that means those locations are stored no matter what, not just when they are selected.
models:
namespace Test\Models;
class Events
{
protected $table = 'test_events';
public $belongsToMany = [
'locations' => [
'Test\Models\Locations',
'table' => 'test_event_locations',
'key' => 'event_id',
'otherKey' => 'location_id',
'pivot' => ['favorite']
]
];
public function beforeSave()
{
//I'm pretty sure I need to do stuff here
// each location that is not marked favorite needs to
// have favorite added to the array wtih a value of 0
}
}
namespace Test\Models;
class Locations
{
protected $table = 'test_locations';
public $belongsToMany = [
'events' => [
'Test\Models\Events',
'table' => 'test_event_locations',
'key' => 'location_id',
'otherKey' => 'event_id',
'pivot' => ['favorite']
]
];
}
HTML demo used to display the data and collect selections
<div>
<form>
<table>
<thead><tr>
<th>Selected</th>
<th>Name</th>
<th>Preferred</th>
</tr></thead>
<tbody>
<tr>
<td><input type="checkbox=" name="Event[locations][1][location_id]" value="1" /></td>
<td>Location 1</td>
<td><input type="checkbox=" name="Event[locations][1][favorite]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox=" name="Event[locations][2][location_id]" value="2" /></td>
<td>Location 2</td>
<td><input type="checkbox=" name="Event[locations][2][favorite]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox=" name="Event[locations][3][location_id]" value="3" /></td>
<td>Location 3</td>
<td><input type="checkbox=" name="Event[locations][3][favorite]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox=" name="Event[locations][4][location_id]" value="4" /></td>
<td>Location 4</td>
<td><input type="checkbox=" name="Event[locations][4][favorite]" value="1" /></td>
</tr>
</tbody>
</table>
<form>
</div>
var_dump of post() with Test 1, 3, and 4 selected with 3 marked preferred
array ()
'Event' =>
array ()
'locations' =>
array ()
1 =>
array (size=1)
'location_id' => string '1' (length=1)
3 =>
array (size=2)
'location_id' => string '3' (length=1)
'favorite' => string '1' (length=1)
4 =>
array (size=1)
'location_id' => string '4' (length=1)
Any thoughts on how to properly adjust this data would be very appreciated, thank you in advance.
Last updated
I found it!!!! I was trying to make changes in the model code, I went back through the formcontroller and checked function names and found I needed to put my changes in the controller under formBeforeSave(). Everything saves and data changes as I expected. Thank you all for looking at this issue with me.
1-2 of 2