Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:44:28+00:00 2026-06-16T00:44:28+00:00

I need to make a fundamental decision of my database/web interaction and I am

  • 0

I need to make a fundamental decision of my database/web interaction and I am missing the knowledge to even find proper search terms.

Background:

I am building a family website which supports a forum, family tree, pvp games with rankings and more details, all from a datamodel. Technologies right now: Php, MySQL, javascript in object oriented fashion.

Requirement:

In a forum datamodel, process a written post as addition of a new forum topic (thread).

Current approach:

In my current datamodel this would imply and update on two tables: Post and Topic.
I would need to insert a row in the topic table, then get the newly generated topicId(sequence), and then use that in an insert to the post table.

Problem:

I feel this is too much work for what needs to happen, too much interaction.
But it will become a typical requirement if I stick with the current approach.

Question:

  1. am I on the right track anyway or should I
  2. restructure the datamodel or
  3. pick another way of database interaction (e.g. stored procedures)
  4. am I facing a typical example where you would use methodology/framework xyz.

Currently tables have following structure (loosely based on this one from erdiagrams.com)

TOPIC: (‘thread’)

id
Forum_ID (FK)
Person_ID (FK)(threadcreator)
IsLocked
IsSticky
Subject
ViewCount
DateCreated
Tc_post_id  - trigger to last post_id in this thread

POST

id
topic_id(FK)
person_id(FK)
subject
message
timestamp
replyto

Then I have a view that collects the last post for each topic and displays some info on that as well (e.g. last poster image) over the trigger Tc_post_id.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:44:29+00:00Added an answer on June 16, 2026 at 12:44 am

    Ad 1 and 2: Your data model is fine. Using foreign keys is crucial here. One more thing that you need to take care of is that the database should ensure there is a TOPIC record for each POST. This is done by setting POST.topic_id NOT NULL attribute. This is sufficient safety mechanism on the DB side, as it ensures that no POST will be left without TOPIC. No matter what you do now with your POST you are obligated to provide a TOPIC.

    Ad 3: A trigger with stored procedure is not recommended here as you have additional data in your TOPIC table (IsSticky, IsLocked, etc), which you might want to provide upon TOPIC record creation. Also, if such a trigger would be applicable, the database design would be a subject to denormalization.

    Ad 4: On the business logic side you can now aid yourself by writing a automated mechanism to create the TOPIC record every time a new POST record is created without specified topic_id. I recommend using some ORM for this or take advantage of the data models available in any MVC framework. The blueprint for such models would look like this:

    abstract class AModel // this class should be provided by ORM or framework
    {
        /**
         * @var PDO
         */
        protected $_db_driver;
    
        public function getLastInsertId()
        {
            $stmt = $this->_db_driver->prepare('SELECT LAST_INSERT_ID() AS id');
            $stmt->execute();
            return $stmt->fetch(PDO::FETCH_OBJ)->id;
        }
    
        public abstract function getFieldList();
    }
    
    class ForumTopicModel extends AModel
    {
        public function insert(array $data)
        {
            $sql = 'INSERT INTO topic VALUES (:id, :forum_id, :person_id, :is_locked, ...)';
            $stmt = $this->_db_driver->prepare($sql);
            return $stmt->execute($data);
        }
    
        public function getFieldList()
        {
            return array('id', 'forum_id', 'person_id', 'is_locked', /*...*/);
        }
    
        // ...
    }
    
    class ForumPostModel extends AModel
    {
        public function insert(array $data)
        {
            $sql = 'INSERT INTO post VALUES (:id, :topic_id, :person_id, :subject, ...)';
            $stmt = $this->_db_driver->prepare($sql);
            return $stmt->execute($data);
        }
    
        public function getFieldList()
        {
            return array('id', 'topic_id', 'person_id', 'subject', /*...*/);
        }
    
        public function insertInitialTopicPost(array $form_data)
        {
            $this->_db_driver->beginTransaction();
    
            $result = true;
    
            if ( empty($form_data['topic_id']) ) {
                // no topic_id provided, so create new one:
                $topic = new ForumTopicModel();
                $topic_data = array_intersect_key(
                    $form_data, array_flip($topic->getFieldList())
                );
                $result = $topic->insert($topic_data);
                $form_data['topic_id'] = $topic->getLastInsertId();
            }
    
            if ( $result ) {
                $forum_post_data = array_intersect_key(
                    $form_data, array_flip($this->getFieldList())
                );
                $result = $this->insert($forum_post_data);
            }
    
            if ( $result ) {
                $this->_db_driver->commit();
            }
            else {
                $this->_db_driver->rollBack();
            }
    
            return $result;
        }
    
        // ...
    }
    

    Note: as a good MVC practice those models should be the only place to directly operate on the table rows. Otherwise you’ll end up getting SQL errors (but the data model will remain coherent, so you don’t have to worry that something will break).

    Finally take advantage of your models in the controller layer:

    class ForumPostController extends AController
    {
        public function createInitialTopicPostAction()
        {
            $form_data = $this->getRequest()->getPost(); /* wrapper for getting
                the $_POST array */
    
            // (...) validate and filter $form_data here
    
            $forumPost = new ForumPostModel();
            $result = $forumPost->insertInitialTopicPost($form_data);
    
            if ( $result ) {
                // display success message
            }
            else {
                // display failure message
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is
I need to make a administrative GUI with lots of functionality like lists, forms,
I need to make a moderate system like the one in fmylife.com. Basically the
I need to make a point in time branch, when we release a new
I need to make efficient d-dimensional points searching and also make efficient k-NN queries
I need to make a series (1-20) ajax calls and I need to have
I need to make a QT app to run on the startup of an
I need to make the top header of a report disappear. The part that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.