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 8543983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:26:31+00:00 2026-06-11T12:26:31+00:00

I’m working on a flash gaming website. I have two models: Game and User

  • 0

I’m working on a flash gaming website. I have two models: Game and User and an intermediate table in which I keep the user’s actions as for example: “User 1 likes Game 3”.

  1. Where is the best place for my like function?
  2. Is it a good practice to grab current user id in Game model? or should I pass it as parameter?

For performance reasons, I increment likes field in game table too.
I’ve omitted to check if user already likes the game just to keep it simple.
Here are my options:

First version:

$user->like(12345);

class User  
{
  public function like($game_id)
  {
    $like = new User_Game();  
    $like->user_id = $this->id;  
    $like->game_id = $game_id;  
    $like->save();  

    $obj = new Game($game_id);  
    $obj->likes++;  
    $obj->save();  
  }
}  

Second version:

$game->like(); // by current user

class Game  
{  
  public function like()  
  {

    $like = new User_Game();    
    $like->user_id = $_SESSION[‘user_id’];    
    $like->game_id = $this->id;    
    $like->save();   

    $this->likes++;    
    $this->save();    
  }    
}    
  • 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-11T12:26:32+00:00Added an answer on June 11, 2026 at 12:26 pm

    To be perfectly honest, I’m not sure if this is the best place for a question such as this. Perhaps codereview is a better fit. All things aside, IMO, neither of the two takes you suggest are “the best approach”. But as always, that might be a personal thing.
    In my view, the best way to go about OOP, is to push all your data in objects ASAP, and implement a service layer that takes care of operations that require multiple queries, or several objects.

    If I may assume you’re using an MVC-ish pattern, your controller receives the data. There, you instantiate a Game object, and set the id to 123456. You can pass that instance to a service method called fillGameModel(Game $gameInstance). This method connects to the DB, and sets all other properties of the Game object and returns it. Same goes for the User object. Both of these objects can then be passed to another service method: likeGame(Game $game, User $user). That method can take care of the rest.
    Personally, I’d go even one step further and use mappers for my DB access, but I’m not going into that now. Here’s an example using a service, and a more OO approach:

    //controller:
    $user = new User();
    $user->setId($_SESSION['user_id']);
    $game = new Game();
    $game->setId(123456);//wherever you get this from
    $service = new MainService();
    $service->userLikes($game,$user);
    
    //service:
    public function userLikes(Game $game, User $user)
    {
        $user = $this->_completeUser($user);
        $game = $this->_completeGame($game);
        //insert or update whatever data you need...
    }
    
    protected function _completeUser(User $user)
    {
        $db = $this->_getConnection();//asuming PDO, to keep things simple
        $query = 'SELECT * FROM my_db.users WHERE id = ?';
        $stmt = $db->prepare($query);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        foreach ($row as $field => $value)
        {//this implies getters and setters in your model
            $user->{'set'.ucfirst(strtolower($field))}($value);
        }
        return $user;
    }
    
    protected function _completeGame(Game $game)
    {
        $db = $this->_getConnection();
        $query = 'SELECT * FROM my_db.games WHERE id = ?';
        $stmt = $db->prepare($query);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        foreach ($row as $field => $value)
        {//field id becomes "setId" method, field name "setName" etc...
            $game->{'set'.ucfirst(strtolower($field))}($value);
        }
        return $game;
    }
    
    //just for show: a pseudo-overloader method, if your models all share the same
    //abstract class. 
    protected function _completeAny(Model_Abstract $model)
    {
        $modelName = get_class($model);
        if (method_exists($this,'_complete'.$modelName))
        {
            return $this->{'_complete'.$modelName}($model);
        }
        throw new Exception('No completion method for '.$modelName.' found');
    }
    

    Again, the loops through the resultset could be replaced with a method in an abstract model class that takes an array as argument, and transforms the fieldnames to their corresponding setters. Plenty of room for abstraction, I’d say 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.