4

I've made several classes to organize the data taken from a database. They are as such:

class.Product.php
class.WineProduct.php (extends Product)
class.ProductAward.php (included in Product, used as array item in Product->awards)
class.ProductReview.php (included in Product, used as array item in Product->reviews)
class.WineList.php (includes WineProduct, uses WineProduct as array item in WineList->items)

The database has a wines table which holds all the relevant data; the trouble I'm having is how to represent the wine category type in the class. In the database it's stored as a numerical value which is tied to a secondary table called _product_types (i.e., 1 corresponds to Red Wine on _product_types). Is it best to store the numerical representation of the category or the textual representation? (1 vs "Red Wine").

On another note, is it better practice to use SQL queries outside of classes?

1
  • What you're making is essentially an ORM - have you looked at some of the existing solutions in this space, such as Doctrine? Commented Jan 24, 2011 at 18:10

1 Answer 1

1

I would go with numeric values mapped as class constants, e.g.

class Product {
    const RED_WINE = 1;
    const WHITE_WINE = 2;
    const BLUSH_WINE = 3;

    public $product_type;
}

$item = new WineProduct;
$item->product_type = product::RED_WINE;
Sign up to request clarification or add additional context in comments.

1 Comment

Is there an easier way to set the constants for the class from the database or would I have to update both the database AND the class file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.