This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

grandpa
grandpa

Hi, I got this error "Call to a member function hasRelation() on null" on my nested relationship. I noticed it only happened during the page 'create', and for the 'update' is working properly. One of my hypotheses is because my model is not instantiated during page 'creation'. What I am planning to do is to instantiate my model in boot() Plugins.php if the page type is 'create' or is there any workaround to deal with this issue?

Plugins.php

SecondModel::extend(function($model) {
     //$model->ThirdModel = new ThirdModel(); it seems this models is not intantiated during the page create. 
     //However, if i instantiate this model is working properly in page create.
     $model->belongsTo['ThirdModel'] = ['model name space'];
});

Event::listen('backend.form.extendFields', function ($form) {
    if (!$form->model instanceof SecondModel)
        return;

     $form->addTabFields([
                 ThirdModel[column_name] => ['...']    
      ]);
});
JeffGoldblum
JeffGoldblum

I'm not sure I follow, can you use the actual names of your models and the actual code you're trying to use instead of the abstraction you posted already?

grandpa
grandpa

Hi LukeTowers, sorry it's my mistake. Please see my sample code below. I hope anyone can follow it. Thank you

Person Model

 public $hasMany = [
      'phone' => 'proj\person\models\Phone',
 ];

Person fields.yaml

    phone:
        label: 'Phone List'
        type: partial
        path: field_phone_list

Person config_relation.yml #injecting phone section so i can add multiple phones to the person

phone:
  label: 'Phone Details'
  view:
    list: $/proj/person/models/phone/columns.yaml
    toolbarButtons: create|add|remove
  manage:
    list: $/proj/merchant/models/phone/columns.yaml
    form: $/proj/merchant/models/phone/fields.yaml
    recordsPerPage: 10

Phones Model

     public $belongsTo = [
         'internetProvider' => 'proj\person\Models\InternetProvider'
     ];

So, when click the create phone details, it will show the popup and from there i created another section to show the Internet Services Provider. However, when i click save it shows call to a member function hasrelation on null it happens only when i create a new person record. If there an existing record it works properly

Plugins.php

  public function boot()
 {
      PhoneModel::extend(function($model) {
         #$model->internetProvider = new InternetProvider(); Having this instantiated it will works properly in `create` page. 
         #But I'm not sure if there is the right way to instantiate this model just intended for
         #`create` page only. Because it will thrown an error if this is instantiated in `update` pages.
          $model->belongsTo['internetProvider'] = ['proj\person\Models\InternetProvider'];
      });
   }    

 Event::listen('backend.form.extendFields', function ($form) {
    if (!$form->model instanceof PhoneModel)
        return;

     $form->addTabFields([
            'internetProvider[isp_name]' => [
                'label' => 'Name',
                'tab' => 'Internet Provider',
            ],
            'internetProvider[isp_plan]' => [
                'label' => 'Plan',
                'tab' => 'Internet Provider',
            ]
       ]);     
 });
mjauvin
mjauvin

In your config_relation.yaml you seem to have a typo for the Manage section... shouldn't it be /proj/person? You used /proj/merchant

mjauvin
mjauvin

Also, in your Phone model's $belongsTo relation, you should add your Person model

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.