Reputation: 11
I'm trying to insert data into multiple related tables from one (builder) back end form/model - but it seems I'm doing something wrong.
t1 posts (used for model Posts.php)
id, flag1, flag2 , created_at, updated_at
t2 post_content (used for model Posts_content.php)
id, content
I've tried expanding the model (Posts.php) used for the form as explained in the relations documentation of octobercms like so:
public $hasOne = ['content' => ['Test\Gcn\Models\Post_content', 'key' => 'id', 'otherKey' => 'id']];
While this does not produce an error when a record is created via the backend controller, no data is actually written to posts_content.
I've also tried to solve it with a proxy field
fields.yaml (of model Posts.php)
post_content[content]:
label: 'Post here'
size: ''
mode: tab
span: full
type: markdown
Posts.php (with the proxy field)
public function formExtendModel($model)
{
/*
* Init proxy field model if we are creating the model
*/
if ($this->action == 'create') {
$model->post_content = new Post_content;
}
return $model;
}
According to the error message, the array needs to be set to be jsonable. But even after that it looks like it's trying to insert the data in the wrong table.
What is the proper way to achieve this? I'm trying to have one form where a user can enter some flags (checkboxes and the like) and a text field which should be inserted into post_content table with the correct id.
I appreciate your time and help, thank you!
Upvotes: 1
Views: 408
Reputation: 1642
From my opinion, you have to do many changes in your structure like below:
1) Add content_id in your posts
table
id, flag1, flag2, content_id, created_at, updated_at
And second table should be contents
table
id, content
content_id will be used to create one to one relationship between Post and Content. For more information about relations click here.
2) Then, you have to define models Post
for posts and Content
for all contents.
In Content model give One to one
relationship.
public $hasOne = [
'post' => 'Test\Gcn\Models\Post'
];
If you give this type of relation, this will find content_id
in your Post model so it set direct relationship between Post and Content.
3) Your form field should be like
In Post model fields.yaml
should be
fields:
flag1:
label: 'Flag 1'
oc.commentPosition: ''
span: left
type: text
flag2:
label: 'Flag 2'
oc.commentPosition: ''
span: auto
type: text
And In Content model fields.yaml
should be
fields:
post:
label: Post
oc.commentPosition: ''
nameFrom: flag1
descriptionFrom: description
span: auto
type: relation
content:
label: Content
size: small
oc.commentPosition: ''
span: left
type: textarea
and columns.yaml
should be
columns:
content:
label: Content
type: text
searchable: true
sortable: true
post:
label: Post
select: flag1
relation: post
searchable: true
sortable: true
For more details about the backend, you can go here...1) form relation 2) column relation.
This all is from my side. Now analyze all this and try your code. And tell me if you have any query.
Thank you.
Upvotes: 0