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] Does ORM support some shortcut operations #2831

Closed
shuqingzai opened this issue Aug 3, 2023 · 17 comments
Closed

[Question] Does ORM support some shortcut operations #2831

shuqingzai opened this issue Aug 3, 2023 · 17 comments

Comments

@shuqingzai
Copy link

shuqingzai commented Aug 3, 2023

如下一些很实用且高频使用的快捷方法,无论是 laravelgorm 都支持这些方法,但在 gf 的 ORM 文档我似乎没有找到,可能是我疏忽了,请问支持吗?

  • first() 、take() 、last() 根据条件获取第一条(根据主键排序 ASC)、获取一条、获取最新一条(根据主键排序 DESC) 记录
  • firstOrNew() || FirstOrInit() 找不到则根据给定的数据创建一个 Entity 实例,但未保存数据库
  • firstOrCreate() 找不到则根据给定的数据创建一个 Entity 实例且自动保存数据库
  • updateOrCreate() 根据给定的条件查找数据,如果存在则更新给定的数据,否则使用给定的数据创建记录

1. What version of Go and system type/arch are you using?

#-> % go version
go version go1.20.6 darwin/amd64

2. What version of GoFrame are you using?

#-> % gf --version
GoFrame CLI Tool v2.5.1, https://goframe.org
GoFrame Version: v2.5.1 in current go.mod

3. Can this issue be re-produced with the latest release?

4. What did you do?

5. What did you expect to see?

6. What did you see instead?

@Issues-translate-bot Issues-translate-bot changed the title [Question] ORM 是否支持一些快捷操作 [Question] Does ORM support some shortcut operations Aug 3, 2023
@wln32
Copy link
Member

wln32 commented Aug 4, 2023

文档里面不是都有吗

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Isn't it all in the document?

@shuqingzai
Copy link
Author

shuqingzai commented Aug 4, 2023

文档里面不是都有吗

我好像没找到,妳能发我链接看看吗? @wln732

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Isn’t it all in the document?

I can't seem to find it, can you send me the link? @wln732 w

@wln32
Copy link
Member

wln32 commented Aug 4, 2023

文档里面不是都有吗

我好像没找到,妳能发我链接看看吗? @wln732

https://goframe.org/pages/viewpage.action?pageId=1114344

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Isn't it all in the document

I can't seem to find it, can you send me the link? @wln732

https://goframe.org/pages/viewpage.action?pageId=1114344

@shuqingzai
Copy link
Author

文档里面不是都有吗

我好像没找到,妳能发我链接看看吗? @wln732

https://goframe.org/pages/viewpage.action?pageId=1114344

这只是 插入数据的保存 ,并没有实现我说的那几个方法 @wln732

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Isn't it all in the document

I don't seem to find it, can you send me the link? @wln732

https://goframe.org/pages/viewpage.action?pageId=1114344

This is just the preservation of inserted data, and does not implement the methods I mentioned @wln732

@wln32
Copy link
Member

wln32 commented Aug 5, 2023

1.firstOrNew() || FirstOrInit() 找不到则根据给定的数据创建一个 Entity 实例,但未保存数据库
搞不明白你这个的逻辑,你这个默认的实例,那他的字段值是多少??go的orm都是让你先手动新建个默认值,然后scan赋值
2.first() 、take() 、last() 根据条件获取第一条(根据主键排序 ASC)、获取一条、获取最新一条(根据主键排序 DESC) 记录
这些不都是写在sql里面的吗
3.updateOrCreate() 根据给定的条件查找数据,如果存在则更新给定的数据,否则使用给定的数据创建记录
使用INSERT INTO语句进行数据库写入,如果写入的数据中存在主键或者唯一索引时,更新原有数据,否则写入一条新数据。
这个是gf orm的文档说明
4. firstOrCreate() 找不到则根据给定的数据创建一个 Entity 实例且自动保存数据库
也就这个没有实现而已,自己可以封装个方法

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


1.firstOrNew() || If FirstOrInit() cannot be found, an Entity instance is created according to the given data, but the database is not saved
I don't understand your logic, your default instance, what is its field value? ? The orm of go allows you to manually create a default value first, and then scan to assign the value
2.first() , take() , last() Get the first record according to the conditions (sort ASC according to the primary key), get one, get the latest record (sort DESC according to the primary key) records
Aren't these all written in sql?
3.updateOrCreate() Find data according to the given conditions, update the given data if it exists, otherwise create a record with the given data
Use the INSERT INTO statement to write to the database. If there is a primary key or unique index in the written data, update the original data, otherwise write a new piece of data.
This is the documentation of gf orm
4. If firstOrCreate() cannot be found, create an Entity instance based on the given data and automatically save the database
It’s just that this has not been realized, you can encapsulate a method yourself

@shuqingzai
Copy link
Author

shuqingzai commented Aug 5, 2023

1.firstOrNew() || FirstOrInit() 找不到则根据给定的数据创建一个 Entity 实例,但未保存数据库 搞不明白你这个的逻辑,你这个默认的实例,那他的字段值是多少??go的orm都是让你先手动新建个默认值,然后scan赋值 2.first() 、take() 、last() 根据条件获取第一条(根据主键排序 ASC)、获取一条、获取最新一条(根据主键排序 DESC) 记录 这些不都是写在sql里面的吗 3.updateOrCreate() 根据给定的条件查找数据,如果存在则更新给定的数据,否则使用给定的数据创建记录 使用INSERT INTO语句进行数据库写入,如果写入的数据中存在主键或者唯一索引时,更新原有数据,否则写入一条新数据。 这个是gf orm的文档说明 4. firstOrCreate() 找不到则根据给定的数据创建一个 Entity 实例且自动保存数据库 也就这个没有实现而已,自己可以封装个方法

  1. 你可以查阅 gorm 的文档示例
  2. 我的主题是快捷高频操作,这几个方法在日常开发中都是属于这一类,按你说的每个查询都自己使用 order by ,相当于很多地方都编写同一段重复逻辑代码,这就不算快捷了
  3. 也许你是对的,gf 使用 replace into 的方式实现 save ,但是如果我的条件不是主键或唯一索引就没法实现
  4. 嗯,如果 gf 没有实现,只能这样,看了文档没找到,才询问的

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


1.firstOrNew() || If FirstOrInit() cannot be found, create an Entity instance based on the given data, but the database has not been saved. I don’t understand your logic. What is the field value of your default instance? ? ? The orm of go allows you to manually create a default value first, and then scan assignment 2. first(), take(), last() Get the first item according to the conditions (sort ASC according to the primary key), get one item, and get the latest item ( Sort according to the primary key (DESC) Records are not all written in sql? 3. updateOrCreate() Find data according to the given conditions, if it exists, update the given data, otherwise use the given data to create records Use the INSERT INTO statement For database writing, if there is a primary key or unique index in the written data, update the original data, otherwise write a new piece of data. This is the documentation of gf orm 4. If firstOrCreate() cannot be found, create an Entity instance based on the given data and automatically save the database

  1. You can refer to the documentation example of gorm
  2. My topic is fast and high-frequency operation. These methods belong to this category in daily development. According to what you said, each query uses order by by itself, which is equivalent to writing the same paragraph repeatedly in many places. Logical code, this is not a shortcut
  3. Maybe you are right, gf uses replace into to implement save
  4. Well, if gf is not implemented, this is the only way to ask.

@wln32
Copy link
Member

wln32 commented Aug 5, 2023

// FirstOrInit这个api完全是多此一举,
db.FirstOrInit(&user, User{Name: "non_existing"})
// user -> User{Name: "non_existing"}
// 为什么不能是这种写法呢
var user = User{Name:"jack"}
db.First(&user)

你上面那种写法,没有找到数据,是不是还要在反射给&user赋值?
下面这种写法才是正常的,没有找到就不做任何动作
gf的orm推崇使用指针来提前声明

var user *User
// 如果没有找到数据,user还是nil什么都不做
db.One(&user)

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


// The FirstOrInit api is completely superfluous,
db. FirstOrInit(&user, User{Name: "non_existing"})
// user -> User{Name: "non_existing"}
// Why can't it be written this way?
var user = User{Name:"jack"}
db. First(&user)

If you did not find the data in the way you wrote above, do you still need to assign a value to &user in reflection?
The following writing method is normal, if you don’t find it, don’t do any action
GF's orm advocates the use of pointers to declare in advance

var user *User
// If no data is found, user or nil do nothing
db. One(&user)  
      

@gqcn
Copy link
Member

gqcn commented Oct 7, 2023

@shuqingzai @wln732 大家好,在goframe里面的快捷封装方法确实不多,目前已提供的基础方法我理解都可以满足开发需要。如果确实有这方面的必要,欢迎参与一起建设。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@shuqingzai @wln732 Hello everyone, there are really not many quick packaging methods in goframe. I understand that the basic methods provided so far can meet development needs. If there is indeed a need for this, you are welcome to participate in the construction together.

@github-actions
Copy link

github-actions bot commented Oct 7, 2023

Hello @shuqingzai. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @shuqingzai。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants