本文整理汇总了Golang中github.com/theduke/go-dukedb.Backend.M2M方法的典型用法代码示例。如果您正苦于以下问题:Golang Backend.M2M方法的具体用法?Golang Backend.M2M怎么用?Golang Backend.M2M使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/theduke/go-dukedb.Backend
的用法示例。
在下文中一共展示了Backend.M2M方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BeforeDelete
func (p PageIntId) BeforeDelete(b db.Backend) error {
// Delete tags.
m2m, _ := b.M2M(p, "Tags")
if err := m2m.Clear(); err != nil {
return err
}
if p.MenuItemId != 0 {
if err := b.Q("menu_items").Filter("id", p.MenuItemId).Delete(); err != nil {
return err
}
}
return nil
}
示例2: TestBackend
//.........这里部分代码省略.........
p := &Project{
Name: "P1",
Todos: []Task{Task{Name: "T1"}, Task{Name: "T2"}},
}
Expect(backend.Create(p)).ToNot(HaveOccurred())
m, err := backend.Q(p).Related("Todos").Find()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeEquivalentTo([]interface{}{&p.Todos[0], &p.Todos[1]}))
})
It("Should .Related() with id", func() {
// Enable auto-create.
rel := backend.ModelInfo("projects").Relation("Todos")
rel.SetAutoCreate(true)
p := &Project{
Name: "P1",
Todos: []Task{Task{Name: "T1"}, Task{Name: "T2"}},
}
Expect(backend.Create(p)).ToNot(HaveOccurred())
m, err := backend.Q("projects", p.Id).Related("Todos").Find()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeEquivalentTo([]interface{}{&p.Todos[0], &p.Todos[1]}))
})
})
Describe("M2M Collection", func() {
It("Should error when building col for unpersisted model", func() {
t := &Task{Name: "test"}
_, err := backend.M2M(t, "Tags")
Expect(err).To(HaveOccurred())
Expect(err.GetCode()).To(Equal("unpersisted_model"))
})
It("Should retrieve collection", func() {
t := &Task{Name: "test"}
Expect(backend.Create(t)).ToNot(HaveOccurred())
_, err := backend.M2M(t, "Tags")
Expect(err).ToNot(HaveOccurred())
})
It("Should .Add()", func() {
t := &Task{Name: "test"}
Expect(backend.Create(t)).ToNot(HaveOccurred())
tags := []Tag{Tag{Tag: "T1"}, {Tag: "T2"}, {Tag: "T3"}, {Tag: "T4"}}
Expect(backend.Create(&tags[0], &tags[1], &tags[2], &tags[3])).ToNot(HaveOccurred())
col, _ := backend.M2M(t, "Tags")
Expect(col.Add(tags[0], tags[1])).ToNot(HaveOccurred())
Expect(col.All()).To(BeEquivalentTo([]interface{}{&tags[0], &tags[1]}))
})
It("Should .Remove()", func() {
t := &Task{Name: "test"}