vt-cwalker
vt-cwalker

Reputation: 805

very versitile PHP dynamic queries

I am trying to abstract as much logic in my PHP classes right from the start. I have the names of all database tables as variables in my DBConnection class. Such as when a user calls $db = new DBConnection(); it auto creates variables for all table names so that in all the queries i have so far I have things like INSERT INTO $db->userTable (fields and constraints) or SELECT (fields) FROM $db->forumTable (constraints). However, how would be a good way to abstract column names where it is very versatile so that Im not hard coding column names into queries as well? so that if something like column names or something are changed, I dont have to go back and edit a ton of queries? Thanks for any input!

Upvotes: 0

Views: 139

Answers (2)

Jim Jose
Jim Jose

Reputation: 1319

Google Appengine has a query language and they have a nice query classimplementation of the same. may be you can follow a similar structure...

Syntax looks something like this,

class Song(db.Model):
    title = db.StringProperty()
    composer = db.StringProperty()
    date = db.DateTimeProperty()

query = Song.all()
query.filter('title =', 'Imagine')

Reference, AppEngine Query Class

Upvotes: 1

Halcyon
Halcyon

Reputation: 57721

Write a database model in some language (possibly of your own choosing), a Domain Specific Language if you will. Use that model to generate your MySQL schema and whatever intermediate model or API you want to offer in PHP.

The model can include things like 'entity' names (not necessarily tables perse), properties and their type (number, text, enumeration, reference).

MySQL is already a very technical implementation and it's not very easy to manipulate. A Domain Specific Language will greatly aid data modeling and data consistency because you get so much for free. You are free of all the details MySQL offers, CHAR, VARCHAR, TEXT, MEDIUMTEXT, LONGTEXT, BLOB. For really generic applications you don't want to think about it.

Update: You want to 'abstract your logic'. You should start with abstracting your datamodel. Have a model that you can transform into MySQL queries that will build your database. You can use the same model to generate an API in PHP. Your model knows which entities there are (tables) and which properties and of which type so you can enforce them in PHP.

Upvotes: 1

Related Questions