简体   繁体   English

使用Laravel的复杂注释,标签,投票,收藏夹数据库关系

[英]Complex Comments, Tags, Votes, Favorites database relationships using Laravel

I am trying to learn HABTM and Polymorphic Relationships by building a small but complex application in Laravel 4.2. 我试图通过在Laravel 4.2中构建一个小而复杂的应用程序来学习HABTM和多态关系。 It takes in links and lets users interact with the content and each other via keywords like tag, comment, vote, favorite, and follow. 它引入了链接,并允许用户通过诸如标签,评论,投票,收藏和关注之类的关键字与内容相互交互。 I have honestly been working on this for about 3 days and feel like I am just spinning my wheels in the mud. 老实说,我已经为此工作了大约3天,感觉就像我在泥泞中转动车轮一样。 Here's the breakdown - 这是细分-

Models: 楷模:

User, Link, Tag, Comment, Vote, Favorite, Follow

Super basic use cases: 超级基本用例:

USER -> ACTION   -> TARGET
User -> Submit   -> Link, Tag, Comment
User -> Tag      -> User, Link
User -> Comment  -> User, Link
User -> Vote     -> Tag, Comment
User -> Favorite -> User, Link, Tag, Comment
User -> Follow   -> User, Tag

(assumed) Tables: (假设)表:

users            [id, username]
links            [id, user_id, url]
tags             [id, user_id, text]
commentables     [id, user_id, commentable, commentable_id, text] //how do i handle comment replies?
taggables        [id, user_id, taggable, taggable_id, weight]
votables         [id, user_id, voteable, voteable_od, vote]
favorables       [id, user_id, favorable, favorable_id]
followable       [id, user_id, followable, followable_id]

From here .. I'm not sure that I'm doing it right. 从这里..我不确定我做对了。 I keep getting caught up in the different ways to define a relationship and I'm just wanting to understand the design completely 我不断以各种方式来定义关系,而我只是想完全了解设计

What do my model files look like? 我的模型文件是什么样的? What cases I might use belongsToMany vs morphToMany? 我可能在什么情况下使用belongsToMany和morphToMany? Is this even the right approach or should I explore using pivot tables? 这是正确的方法还是我应该探索使用数据透视表? I would appreciate some pointers on what I need to be learning to implement this correctly 我将对我需要学习以正确实现此目标的一些建议表示赞赏

Here is what my User.php will probably look like, but I'm not sure what the returns should actually be since I don't quite understand the relationship's wants/needs 这是我的User.php可能的样子,但是由于我不太了解这种关系的需求,我不确定实际的回报率是多少

(assumed) User.php (假设)User.php

usersFavored()    // Users which this User has Favored
usersFollowed()   // Users which this User has Followed
linksSubmitted()  // Links created by the User in the DB
linksFavored()    // ...
tagsOnSelf()      // Tags attached to this User by other Users
tagsCreated()     // Tags which the User created in the DB
tags()            // Tags which the User was the first to attach to a Link
tagsVoted()       // Tags on which the User has Voted across the entire site
tagsFavored()     // ...
tagsFollowed()    // ...
commentsOnSelf()  // ...
comments()        // Comments across the site
commentsFavored() // ...
votesOnSelf()     // ...
votes()           // Votes across the site
favorites()       // Favorites across the site
follows()         // Follows across the site

I feel like I'm close to making this work. 我觉得我即将完成这项工作。 I have the views and routes ready because I've rebuilt this project about four times. 我已经准备好了视图和路线,因为我已经重建了该项目大约四次。 I just need to wrestle all of this database relationship logic so I can make the app come to life. 我只需要努力解决所有这些数据库关系逻辑,就可以使该应用程序栩栩如生。 Your input is much appreciated! 非常感谢您的输入!

here are some of my thoughts 这是我的一些想法

  1. I was really confused about whether to use belongsToMany vs morphToMany. 对于是否使用belongsToMany和morphToMany,我感到非常困惑。 But now as I get more familiar with Laravel, it becomes clear to me that what morphToMany can do can be done by belongsToMany too. 但是现在随着我对Laravel的更加熟悉,对我来说很清楚morphToMany可以做什么也可以由belongsToMany完成。 So you need only master belongsToMany. 因此,您仅需掌握mastersToMany。 It's powerful enough. 它足够强大。 However, if you want to know the difference between the two relationships, here's the explanation on when you should use either of them. 但是,如果您想知道两种关系之间的区别,以下是有关何时应使用它们中的任何一种的说明。 For belongsToMany, you just want to fetch some entities and you do not care about their states. 对于belongsToMany,您只想获取某些实体,而不必关心它们的状态。 However, if you need to differentiate the states between those entities such as votes(two types: up vote and down vote), you could use morphToMany. 但是,如果您需要区分这些实体之间的状态,例如投票(两种类型:向上投票和向下投票),则可以使用morphToMany。
  2. For your assumed User.php, each of those functions should just return a sql query instance. 对于您假定的User.php,这些功能中的每个功能都应仅返回一个sql查询实例。 These functions are used in controller to get results. 这些功能在控制器中用于获取结果。 Simply put, functions in models are just different sql statements wrapped well to use in controllers. 简而言之,模型中的函数只是包装良好的不同sql语句,可在控制器中使用。 In this way you separate business logic from sql selection statements. 这样,您可以将业务逻辑与sql选择语句分开。
  3. When you call functions in models, make sure you call ->get() to ensure the execution of sql statement. 在模型中调用函数时,请确保调用-> get()以确保sql语句的执行。 In essence, each functions in models only return an unexecuted sql statement. 本质上,模型中的每个函数仅返回未执行的sql语句。 If you want to get the results of sql statements, you need to call ->get(). 如果要获取sql语句的结果,则需要调用-> get()。

Hope this helps. 希望这可以帮助。 Ready to help if you have more questions 如果您还有其他问题,随时可以提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM