PHP OOP Question

I was just looking for some advice, currently my DB design is that one user has many blog posts. Now I have a user class and a blog class.

However, I am not sure of the correct way to get all the blog posts for this user. I create a getAllBlogs () function in a user class that returns blog objects, or I create a main blog class that you can search for from a user, so that would be getAllBlogsForUser ($ id)

Thanks: -)

+3
source share
5 answers

. , , (-) . DB . , User::getAllBlogs() Blog::getAllBlogsForUser().

+6

. , , .

, :

<?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;
    }
}

, , . $force .

+2

, . , db , . , user_id - , , . , . get posts , .

+1

, User::getPosts Posts::findByUserId ( "", , "" ). , , frontend , "finder" .

0

, . :: getBlogs() Blogs:: getBlogsByUserId ($ idUser), , , Blogs.

0

Source: https://habr.com/ru/post/1719923/


All Articles