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 have Campaign Model, when you fill the backend form I need to create a Backend user but I get error "Call to a member function hasRelation() on null" on line 70". It makes sense as the Backend user is not yet created and there is no relationship but I do not know how to circumvent that:
One on one relation:
Campaign Class:
public $belongsTo = [
'affiliate_user' => ['Backend\Models\User']
];
Backend user class extended:
$model->hasOne['affiliate'] = ['AristaMedia\AffiliateCampaigns\Models\Campaign', 'key' => 'affiliate_user_id' ];
In Campaign fields.yaml I have 3 fields related to the Backend user:
'affiliate_user[first_name]':
label: 'First Name'
span: auto
type: text
comment: 'Unique Username used to log into backend'
tab: 'Contact Information'
'affiliate_user[last_name]':
label: 'Last Name'
span: auto
type: text
tab: 'Contact Information'
'affiliate_user[email]':
label: 'Login Email'
span: auto
type: text
comment: 'Unique User email to log into backend'
tab: 'Contact Information'
And on Campaign Class I create the new user:
public function afterCreate()
{
$randpass = \Str::random(6);
$user = \BackendAuth::register([
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->contact_email,
'login' => $this->contact_email,
'password' => $randpass,
'password_confirmation' => $randpass,
]);
$affiliateRole = \Backend\Models\UserRole::where('code','affiliates')->first();
$user->role = $affiliateRole;
$user->save();
$this->affiliate_user_id = $user->id;
$this->save();
}
Any help will be greatly appreciated ... I´m a bit stuck and I can´t see how to solve this.
Last updated
I found the solution, I need to intitialise the backend user model to be able to use the proxy fields like affiliate_user[email].
To do that you need to extend the model by adding this function in the Campaign controller:
public function formExtendModel($model){
/*
* Init proxy field model if we are creating the model
*/
if ($this->formGetContext() === 'create' && !$model->affiliate_user ) {
$model->affiliate_user = new \Backend\Models\User;
}
return $model;
}
One very silly mstake I made was adding the funtion to the model :-(
This post helped me: https://octobercms.com/forum/post/howto-create-two-models-on-a-single-form-a-guide-to-proxy-fields
Last updated
1-3 of 3