John Smith
John Smith

Reputation: 6207

Php, where to put multiple database operation logic?

we have a forum_threads and forum_posts tables. Posts connects to threads. An user wants to start a new thread with a first post, so:

class ForumPostModel
{
    public function open ($topic, $firstComment)
    {
        $newThreadId = ForumThreadModel::createNew ($topic); // this will return with the ID of the new thread
        return $this->insertInto ($newThreadId, $firstComment);
    }
}

this pseudo-code shows that first insert a thread and getting its ID, then insert the comment to it and also return with the new ID. Apart from missing transaction and checking, this code is not "clean" doesnt it? Where to put it? To controller, I dont think so.

Upvotes: 0

Views: 61

Answers (1)

Fedir Petryk
Fedir Petryk

Reputation: 497

Where to put your database logic is depends on what your system architecture is right now.

It's a good approach to separate business and database logic into a different layers. It is also good to have a service layer (domain facade with collection of slim interfaces to access them) where you can operate different domain models. So it can be decided to put above code into a service layer of your application.

Here is a good explanation of how it should look service layer

I can advise you to read Martin Fowler's "Patterns of Enterprise Application Architecture".

Upvotes: 1

Related Questions