This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Did you ever get a solution to this? I have the same problem - adding manually to database allows me to update, but I can't create a new user, edit a user unless without there being an existing entry in the new table.
I came across this forum post and wanted to share what worked for me. I kept getting a null reference on save, and found that I needed to instantiate an instance of my model if it did not exist for both create and update.
Plugin.php:
public function boot()
{
// hook in to Form model and add our relationship to SimpleTicket table
Form::extend(function ($model) {
$model->hasOne['simpleticket'] = [SimpleTicket::class, 'key' => 'form_id'];
// hook in to form delete to clean up associated simpleticket records
$model->bindEvent('model.afterDelete', function() use ($model) {
$st = SimpleTicket::where('form_id', $model->id);
if ($st) {
$st->delete();
}
});
});
// extend the Form Builder admin to show the SimpleTicket options
Event::listen('backend.form.extendFields', function($widget) {
// Only for the Form Builder - Edit Form screen
if (!$widget->getController() instanceof Forms) {
return;
}
// Only for the Form model
if (!$widget->model instanceof Form) {
return;
}
// if we are doing a create, we need to attach an empty simpleticket to the widget model
// this prevents a null relationship on save
if ($widget->getContext() == 'create' || $widget->getContext() == 'update') {
if (!$widget->model->simpleticket) {
$widget->model->simpleticket = new SimpleTicket();
}
}
// Add SimpleTicket tab and fields
$widget->addTabFields([
'simpleticket[enabled]' => [
'label' => 'Enable SimpleTicket integration',
'comment' => 'Sends the form to a SimpleTicket instead of email',
'tab' => 'SimpleTicket',
'type' => 'checkbox',
'span' => 'full'
],
'simpleticket[ticket_type]' => [
'label' => 'Ticket Type',
'comment' => 'The SimpleTicket ticket type',
'tab' => 'SimpleTicket',
'type' => 'text',
'span' => 'auto'
],
'simpleticket[problem_type]' => [
'label' => 'Problem Type',
'comment' => 'The SimpleTicket problem type',
'tab' => 'SimpleTicket',
'type' => 'text',
'span' => 'auto'
],
'simpleticket[created_by]' => [
'label' => 'Created By',
'comment' => 'Sets who submitted the ticket',
'tab' => 'SimpleTicket',
'type' => 'text',
'span' => 'auto',
'default' => 'Web User'
]
]);
});
}
I know this was an old thread, but I still found it useful. Hopefully this helps you guys solve your problem as well.
I have some update to this if someone is still interested. I wouldn't accept that the form was being extended only on update, and not on create. The issue here is that when you are creating a new item, it doesn't have an id yet. So the subgroup won't have a user_group_id. Hence the quickfix to not extend the form unless it's an update. Here's my work-around to that:
UserGroup::extend(function($model){
$model->hasOne['subgroup'] = ['Codebugs\Ldap\Models\Subgroup'];
$model->bindEvent('model.afterDelete', function() use ($model) {
$sg = Subgroup::where('user_group_id', $model->id);
if($sg) $sg->delete();
});
$model->bindEvent('model.afterCreate', function() use ($model) {
$id = $model->id;
$model->subgroup->user_group_id = $id; // Manually set the parent id to the subgroup
$model->subgroup->save();
});
});
hey i want to create a model for save the form but i dont know where to create the model please help me out