0

i have managed to put form and list in the same page by reviewing this but after data insert to database, it did not show in the list i have implement these to controller

public $implement = [
    'Backend\Behaviors\ListController',
    'Backend\Behaviors\FormController',
    'Backend\Behaviors\RelationController'];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $relationConfig = 'config_relation.yaml';

and set config for relation

jabatans:
  label: 'Jabatan'
  manage:
        form: $/mismaiti/mywarta/models/jabatan/relation_fields.yaml
        list: $/mismaiti/mywarta/models/jabatan/relation_columns.yaml
  view:
        list: $/mismaiti/mywarta/models/jabatan/relation_columns.yaml
        toolbarButtons: create|delete

and create relationship to parent model

public $hasMany = [

    'jabatans' => [
        'Mismaiti\MyWarta\Models\Jabatan',
        'key' => 'jabatan_id',
        'otherKey' => 'id'
    ]
];

and to child model

public $belongsTo = [
    'jabatan' => [
        'Mismaiti\MyWarta\Models\WartaDataAtribut',  
        'key' => 'jabatan_id'
    ]
];

child form relation_field.yaml

fields:
id:
    label: ID
    span: left
    oc.commentPosition: ''
    hidden: 1
    type: number
jabatan:
    label: 'Fungsi/Jabatan dalam Pelayanan'
    span: right
    oc.commentPosition: ''
    type: text

parent form parentfield.yaml

fields:  
  jabat:
    label: 'Data Jabatan Pelayan'
    span: left
    type: section
  jabatans:
    label: ''
    span: left  
    type: partial
    path: ~/plugins/mismaiti/mywarta/controllers/wartadataatribut/_jabatans.htm

it works with the form but when i insert data to child model and save it.. the data did not show in the list.. am i missing something..

Isral Bustami
  • 137
  • 13
  • 1
    hmm, when you save data to child model , is it persisted in child model means .. `Mismaiti\MyWarta\Models\Jabatan` get record inserted in to db when you list it (dont check relations here just normal list of `Jabatan`). is it saved there or not. – Hardik Satasiya Feb 24 '18 at 10:45
  • other thing can you confirm that this behavior is on both scenario `insert new record` and `update record` is this happen in bot scenarios ? – Hardik Satasiya Feb 24 '18 at 10:46
  • Data is saved to db but it wont show up in the list for child model.. – Isral Bustami Feb 24 '18 at 11:05
  • I can only insert single value on `update record` when trying to create new value for child model return an error that tell can not use the same id twice.. so its only can be done in `create record` – Isral Bustami Feb 24 '18 at 11:10
  • 1
    ok, insert new record and update record .. means i am taking about parent model. if you don't add data to this field and save parent model can be able to save now if you try to update parent model and then this field .. at that time is it working ? (and you are using partial to render this relation right ?) – Hardik Satasiya Feb 24 '18 at 11:29
  • Yes i use partial to render this.. and when i create record and i create child item is working fine but when i save parent model.. child list is empty but it is exist in db .. and other field in parent model work fine.. – Isral Bustami Feb 24 '18 at 11:40
  • I tried to add `key` in relation.. it result all the child value change to numeric value.. making me more lost.. – Isral Bustami Feb 24 '18 at 11:43
  • hmm code seems ok.. to me can you share schema details of both tables – Hardik Satasiya Feb 24 '18 at 12:06
  • sure will update my question, i wonder is it require for child model has its own controller since i dont create it yet.. and do we need to create pivot table for `relationController` because suddenly the data saved to field that i dont create and define.. – Isral Bustami Feb 26 '18 at 04:09
  • 1
    No we dont need child's controller .. and pivot only needed when we need to store info for every relation and though it will hold extra info for many to many relation .. here for one to many we dont need it as child record it self has all info so... Your code is correct but not sure why its not working .. – Hardik Satasiya Feb 26 '18 at 06:34
  • i also wonder where is the missing link.. if i go to `update record` it save the id of the parent model instead of the value i enter.. i notice that in error message indicate that it insert only two field `id of child` and `id of parent model' that open.. that why i assume it need pivot table.. it doesnt save field to field where it supposed to be in db.. – Isral Bustami Feb 26 '18 at 07:43
  • if you see i put key `jabatan_id` right.. i never create field or define this.. it appears in the error message that SQL Command is `INSERT(id,jabatan_id)value(1, item)` so i create new column in db.. – Isral Bustami Feb 26 '18 at 07:54
  • is that how the `relationController` works.. automatically create field base from the model name.. – Isral Bustami Feb 26 '18 at 07:59
  • i have update my question along i put yaml file for both parent and child – Isral Bustami Feb 26 '18 at 08:18
  • hmm schema means `table schema` it will be inside `update` means id, name, title this fields defined for database that schema sorry for this much info but not sure what is issue here so need more info – Hardik Satasiya Feb 26 '18 at 12:20
  • 1
    Finally .. i have find the solution.. child database need to have column for parent record id.. its just as simple as that.. and i spent weeks to solve it.. thank you @HardikSatasiya for your guidance.. – Isral Bustami Mar 06 '18 at 15:25

0 Answers0