1

I think perhaps my questions have been answered in part in other locations on stackoverflow, but I felt I might get a more comprehensive answer by asking it all together. I apologize for anything dumb I might type--I don't work in PHP nearly enough, so when I do, I feel like I'm learning it all over again.

Anyway, let's say I've got a mysql database table called "items" with the following columns: item_id, item_type, item_name. And I've got a PHP class that looks like this:

class Item
{
    public $item_id;
    public $item_type;
    public $item_name;
}

In other words, I really just want a direct map from the database to the class object.

I've decided to include a method in the class called "load" that retrieves the item data when supplied with the item_id. So here are my two questions:

  1. What is the best way to connect to the database within the class? Should I be instantiating a new PDO object inside the class, or should I be passing the PDO database handle in as a parameter on the load method? Does it matter?
  2. Does this make any sense as a way to assign the class properties, or is there a much smarter way to do this:

    $sth = $dbh->query("SELECT * FROM items WHERE item_id = $item_id");
    $sth->setFetchMode(PDO::FETCH_CLASS, 'Item');
    while ($obj = $sth->fetch()) {
        foreach ($obj as $key => $value) {
            $this->$key = $value;
        }
    }   
    

Edit: I realized after seeing these responses (which make for interesting reading, to be sure), that I probably didn't go into enough detail.

I think using an ORM library might be overkill for what I'm doing (just a personal hobby project--not something for an employer), but actually, I'm not certain that's quite what I need, anyway. Here's why. Because I might have something like this:

class InventoryItem extends Item
{
    public $user_id;
    public $quantity;
}

I haven't really thought this through. Just giving an example. So the query I'd use to get the data might look something like this:

SELECT * 
FROM items, inventories 
WHERE item.item_id = inventories.item_id 
AND user_id = $user_id;

In other words, I'd have to create the SQL statement that would correctly pull out the data for the class, and I really just need to know if I should be passing in my database handle or creating it inside the load method, and if the code above makes sense as a good way to assign the property values.

An additional wrinkle is that I might have to manipulate some of the data (e.g., if maybe one of the columns is storing a json encoded array or something).

zajonc
  • 1,935
  • 5
  • 20
  • 25
thewatcheruatu
  • 242
  • 2
  • 9
  • 1
    Search for Row Data Gateway and Active Record. Tentatively a duplicate of http://stackoverflow.com/questions/2296338/oop-design-how-to-incorporate-db-handling-into-application-objects. I won't cast it though because my vote is binding. – Gordon Jul 02 '13 at 08:55
  • It looks like an active record type pattern: https://en.wikipedia.org/wiki/Active_record_pattern .. You might want to consider using phpactiverecord? http://www.phpactiverecord.org/ – Nanne Jul 02 '13 at 08:56
  • possible duplicate of [Good PHP ORM Library?](http://stackoverflow.com/questions/108699/good-php-orm-library) – Burhan Khalid Jul 02 '13 at 09:05
  • Thanks for the information. I realized I hadn't really explained myself well, so I edited the question. Maybe it doesn't change anything. ORM isn't light reading, so I haven't quite wrapped my head around it yet. – thewatcheruatu Jul 02 '13 at 21:44

2 Answers2

1

I use a database object class that is generic, then I have individual classes (which match the different tables in the database) which each extend that.

The database object class contains the functions to instantiate the rows into objects, and perform any crud, the classes that extend them just contain the attributes of each table object.

This might be better asked over on programmers.stackexchange, as its quite an open ended question, and I imagine there are many different ways to do this and opinions on the matter.

Djave
  • 8,595
  • 8
  • 70
  • 124
  • Thanks for the response. You may be right--I probably asked this in the wrong place, since my method works, but I'm just not sure it's a good practice. – thewatcheruatu Jul 02 '13 at 22:26
0

You are looking for an object relational mapper (orm). This is a library let you define objects which map to database tables.

Doctrine seems to be the preferred ORM for PHP. There are others as well.

Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks. Pretty interesting and definitely something upon which I need to read up. Had to edit my question, though, because I think I oversimplified the example, and probably didn't fully explain my need. – thewatcheruatu Jul 02 '13 at 21:45