简体   繁体   English

PHP OOP问题

[英]PHP OOP question

i was just looking for a bit of advice, currently my DB design is that one user has many blog postings. 我只是在寻找一些建议,目前我的数据库设计是一个用户有很多博客帖子。 Now i have a user class and a blog class. 现在我有一个用户类和一个博客类。

However im not sure about the proper way to get all blog posts for that user. 但是我不确定为该用户获取所有博客帖子的正确方法。 Do i create a getAllBlogs() function in the user class which returns blog objects or do i create a main blog class that you can search by user, so it'd be getAllBlogsForUser($id) 我是在用户类中创建一个getAllBlogs()函数,它返回博客对象,还是创建一个可以按用户搜索的主博客类,所以它是getAllBlogsForUser($ id)

Thanks :-) 谢谢 :-)

I'd personally use the later option. 我个人使用后面的选项。 This is because the blog class knows how to deal with the blog table(s). 这是因为博客类知道如何处理博客表。 I wouldn't want to write blog-specific DB code in the user class. 我不想在用户类中编写特定于博客的数据库代码。 On the other hand, you still can add User::getAllBlogs() as a wrapper around Blog::getAllBlogsForUser() . 另一方面,您仍然可以将User::getAllBlogs()添加为Blog::getAllBlogsForUser()的包装器。

It's really up to you. 这完全取决于你。 Since users are pretty tightly coupled to blogs, you could do both. 由于用户与博客紧密相关,因此您可以同时执行这两项操作。

Consider how nicely they could play together: 想想他们可以一起玩得多好:

<?PHP
class User {
    protected $_blogs;

    public function getBlogs($force=false){
        if (empty($this->_blogs) || $force){
            $this->_blogs = BlogClass::getBlogsByUser($this->user_id);
        }
        return $this->_blogs;
    }
}

Now you can grab a user's blogs whenever you want, and it will fetch them only when necessary. 现在,您可以随时获取用户的博客,并且仅在必要时才会获取它们。 The $force parameter can be used to force a reload. $ force参数可用于强制重新加载。

i am not sure if this is actually a helpful answer .. however if your db design is done correctly with the correct normalization practices in place .. it should be indicative of your class layouts as well. 我不确定这是否真的是一个有用的答案..但是,如果您的数据库设计正确完成正确的规范化实践..它应该表明您的类布局。 as User is an independent entity, and posts use user_id or something as the foreign key , it should clearly give you the idea who should be the master. 由于User是一个独立的实体,并且帖子使用user_id或其他东西作为外键,它应该清楚地告诉你谁应该是主人。 Users can exist with-out posts , however posts can not exist with out users. 用户可以存在不带帖子的帖子,但是用户不能存在帖子。 So it makes sense to put the get posts method in the postings class rather than the users class. 因此,将post posts方法放在postss类而不是users类中是有意义的。

I'd use both, like User::getPosts and Posts::findByUserId ("Posts" as model name sounds better to me than "Blogs" ). 我使用两者,比如User::getPostsPosts::findByUserId (“帖子”作为模型名称听起来比“博客”更好)。 Both methods have similar functionality and which one to use depends on the context, for example, frontend will rather use Users model, while in admin interface 'finder' methods could be more appropriate. 两种方法都具有相似的功能,使用哪种方法取决于上下文,例如,前端将使用用户模型,而在管理界面中,“finder”方法可能更合适。

I would create both. 我会创造两者。 Your User::getBlogs() could use Blogs::getBlogsByUserId($idUser) since when you are in the user context, you will have access to the user's id to pass to the Blogs method. 您的User :: getBlogs()可以使用Blogs :: getBlogsByUserId($ idUser),因为当您在用户上下文中时,您将可以访问用户的ID以传递给Blogs方法。

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

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