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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:32:50+00:00 2026-05-27T00:32:50+00:00

Here is my model_users class (the model it extends from doesn’t have anything yet

  • 0

Here is my model_users class (the “model” it extends from doesn’t have anything yet – still trying to work on getting this one ready and then abstracting out common functions for use with other models), Database::query just returns mysql_fetch_assoc() of the result:

class Model_User extends Model {    
    private static $_table = 'users';

    public function __construct($properties = array())
    {
        parent::__construct();
        foreach ($properties as $key => $value) {
            $this->{$key} = $value;
        }
    }

    public function exists()
    {
        return ($this->id) ? TRUE : FALSE;
    }

    public static function get_by_id($id = NULL)
    {
        if ($id)
        {
            $result = Database::query(
                sprintf('SELECT * FROM ' . self::$_table . ' WHERE id = %d', $id)
            );

            return new Model_User($result);
        }
        else
        {
            return new Model_User(array());
        }
    }

    public function save()
    {
        return TRUE;
    }
}

This allows me to do something like:

$u = Model_User::get_by_id(5);
        if ($u->exists())
        {
            echo $u->name;
            echo $u->id;
        }
        else
        {
            echo 'No user';
        }

I’m trying to learn how to use models without an orm or anything like it (I’ve used those before, but I want to learn how to do a simple version of them).

I’m wondering how to keep track of properties of a model in an abstract way, I’ve seen php’s reflection object and that might be what I need. I just want to be able to do things like:

$u = new Model_User();
$u->name = 'John';
$u->email = 'john@example.com';
$u->save();

or like

$u = Model_User::get_by_id(2);
$u->email = 'someone@example.com';
$u->save();
// Different than the prior one since it would UPDATE, not INSERT

I’m not asking how to implement the entire save() method, but I need to know how to think about this in a way that can allow me to have an abstract model class that is easy to use. One of the barriers I need to get over is that there’s two types of properties, one type that is an actual property of the model (in this case ‘name’ or ’email’) and another type which has to do with other things such as ‘table name’ or maybe even some ‘has many’ relationships information.

  • 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-05-27T00:32:51+00:00Added an answer on May 27, 2026 at 12:32 am

    One answer is: metadata.

    You need to keep track of some data about your data.

    If you want to have your db-columns exposed as plain-vanilla public properties on your model (and you don’t want to get into magic _get()/_set() stuff), you need to maintain a list, somewhere, of which properties need to be read/written from/to the db.

    You could have something like;

    <?php
    class User extends Model {
        public $dbcols = array(
            'id',
            'name',
            'email'
        );
    }
    

    You save() method would then examine $this->dbcols to figure out what data it needs to worry about. In the above example, an update() method would do something like:

    function update(){
        $updates = array();
        foreach($this->dbcols as $name){
            $updates[$name] = "'{$this->escape($this->$name))}'";
        }
        $sql = "UPDATE {$this->tableName} SET " . implode(',',$updates) . " WHERE id={$this->id}";
        ...
    }
    

    Of course, you could make $dbcols an associative array, and have different identifiers for your properties and column names, add validation or escaping rules, and eventually make things very complicated.

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

Sidebar

Related Questions

Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =
Here's my use case : public class User extends Model {} public class TableA
here is my model class gameUserTrophyInfo(models.Model): user = models.ForeignKey(userInfo) trophy = models.ForeignKey(gameTrophyInfo) date =
I've got an interesting box-model problem here. I have a header full of links,
Here are the domain model classes: public abstract class BaseClass { ... } public
Here's a Django model class I wrote. This class gets a keyerror when I
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
Here is a simplified version of my database model. I have two tables: Image,
I'm having difficulty understanding what's going on here class AppController extends Controller { ...
I have a class that doesn't extend webapp.RequestHandler , and I can't use self.response.out.write()

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.