This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
If you need to save two models using a form controller, you may run into some issues. However, it is possible! The key is to instantiate the related models manually.
Assume you have a Person model and a Phone model. On your People form controller you want to create/update the Phone model, but you don't want it to look like it's a separate record. Proxy fields to the rescue!
#Acme/Anvil/Models/Person/fields.yaml
fields:
name:
label: Name
commentAbove: Text field, required. Given name in the first box, preferred name in the second box.
# Proxy Fields
phone[name]:
span: auto
label: Phone Name
phone[number]:
span: auto
label: Phone Number
Shown above are two proxy fields which are denoted by the nested keys. Note that phone[] is the name of the relation on the Person model.
Once your proxy fields are defined, there's just one more important step: manually instantiating the related model.
//Acme/Anvil/Controllers/People.php
...
public function formExtendModel($model)
{
/*
* Init proxy field model if we are creating the model
*/
if ($this->action == 'create') {
$model->phone = new Phone;
}
return $model;
}
...
And viola! You can now save data on multiple models at a time. Note that this only works for a single related model (1 phone to 1 person), for more than one, use a Relation behavior.
See also: the People controller of the October Test Plugin
thank you jwilson8767. Very usefull!
or all of you can try this code if you want to extend some plugin. For example, i want to add image and location to KurtJensen.MyCalendar plugin.
- create your plugin
- add this code to your plugin.php
public function boot()
{
EventModel::extend(function($model) {
$model->hasOne = [
'place' => ['Rahman\Calendar\Models\Event']
];
$model->attachOne = [
'featured_image' => ['System\Models\File']
];
$model->bindEvent('model.beforeDelete', function() use ($model) {
$model->place && $model->place->delete();
});
});
EventController::extendFormFields(function($widget, $model, $context) {
if (!$model instanceof EventModel)
return;
if ($context == 'create')
$model->place = new Event;
$widget->addFields([
'featured_image' => [
'label' => 'Featured image',
'type' => 'fileupload',
'mode' => 'image',
'imageWidth' => 260,
'imageHeight'=> 260,
'span' => 'auto'
],
'place[location]' => [
'label' => 'Location',
'span' => 'auto',
]
]);
});
}
That's it!
Hi everyone. I'm having some troubles for use the proxy fields with fileupload and a help would be very apreciated.
With this in my controller ...
public function formExtendModel($model)
{
if ($this->action == 'create') {
$openingMessage = new Message();
$openingMessage->creator = BackendAuth::getUser();
$openingMessage->file = new File();
$model->message = $openingMessage;
}
return $model;
}
... and this in my fields.yaml ...
message[content]:
label : Message
type : textarea
context : ['create']
message[file]:
label : File
type : fileupload
context : ['create']
.. I got this ...
Call to undefined method Illuminate\Database\Eloquent\Collection::hasRelation()
.. am i doing something wrong ?
Last updated
I may be 5 years too late but I have to thank you on this one, this issue got me stuck for a while !
1-6 of 6