Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How create related entities objts in one form? #619

Open
Todvaa opened this issue May 22, 2024 · 0 comments
Open

[Question] How create related entities objts in one form? #619

Todvaa opened this issue May 22, 2024 · 0 comments

Comments

@Todvaa
Copy link

Todvaa commented May 22, 2024

Description

Greetings! I have two related entities whose attributes I display together. Is there a way to create them together in a form? Or make two forms for creating these entities in a row. Thank You!!!

Example code

package models

import (
	"gorm.io/gorm"
)

type Article struct {
	gorm.Model // adds ID, created_at etc.

	SitePosition int `json:"site_position" gorm:"uniqueIndex:page_site_position_uniq"`

	PageName string `json:"page_id" gorm:"type:varchar(25);not null;uniqueIndex:page_site_position_uniq"`
	Page     Page   `json:"page" gorm:"foreignKey:PageName;references:Name"`
}

type ArticleTranslation struct {
	gorm.Model // adds ID, created_at etc.

	Text string `json:"text" gorm:"type:text"`

	LocalizationName string       `json:"localization_name" gorm:"type:char(2);not null;uniqueIndex:localization_article_uniq" validate:"required"`
	Localization     Localization `json:"-" gorm:"foreignKey:LocalizationName;references:Name"`
	ArticleID        uint         `json:"article_id" gorm:"uniqueIndex:localization_article_uniq"`
	Article          Article      `json:"-" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
package tables

import "github.com/GoAdminGroup/go-admin/template/types"

const (
	ID             = "Номер"
	TranslationsID = "Номер перевода"
	Text           = "Текст"
	Localization   = "Язык"
	SitePosition   = "Позиция на сайте"
	Page           = "Страница размещения"
)
const (
	Articles     = "Тексты"
	ArticlesDesc = "Отдельные тексты на страницах"
)

var LocalizationOptions = types.FieldOptions{
	{Text: "Русский", Value: "ru"},
	{Text: "English", Value: "en"},
}

var PageOptions = types.FieldOptions{
	{Text: "Главная страница", Value: "main"},
	{Text: "Работа у нас", Value: "jobs"},
}
package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	_ "github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	_ "github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetArticlesTable(ctx *context.Context) table.Table {

	articles := table.NewDefaultTable(ctx, table.Config{
		Driver:     db.DriverPostgresql,
		CanAdd:     true,
		Editable:   true,
		Deletable:  true,
		Exportable: true,
		Connection: table.DefaultConnectionName,
		PrimaryKey: table.PrimaryKey{
			Type: db.Int,
			Name: "id",
		},
	})

	info := articles.GetInfo()

	info.SetTable("article_translations").SetTitle(Articles).SetDescription(ArticlesDesc)
	info.AddField(ID, "article_id", db.Int)
	info.AddField(Text, "text", db.Text)
	info.AddField(Localization, "localization_name", db.Text)
	info.AddField(SitePosition, "site_position", db.Int).FieldJoin(types.Join{
		Table:     "articles",
		Field:     "article_id",
		JoinField: "id",
	})
	info.AddField(Page, "page_name", db.Varchar).FieldJoin(types.Join{
		Table:     "articles",
		Field:     "article_id",
		JoinField: "id",
	})
	info.AddField(TranslationsID, "id", db.Int)

	formList := articles.GetForm()
	formList.SetTable("article_translations").SetTitle(Articles).SetDescription(ArticlesDesc)
	formList.AddField(ID, "article_id", db.Int, form.Default).FieldDisplayButCanNotEditWhenUpdate().FieldDisableWhenCreate()
	formList.AddField(Localization, "localization_name", db.Text, form.SelectSingle).
		FieldOptions(LocalizationOptions).FieldDisplayButCanNotEditWhenUpdate()
	formList.AddField(Text, "text", db.Text, form.Text)

	SitePosition ????

	Page ????

	return articles
}
@Todvaa Todvaa changed the title How create related entities objts in one form? [Question] How create related entities objts in one form? May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant