开发者

Approach to data wrapping

I'm developing in PHP and MySQL.

The information about the currently logged in user is stored in many different tables. The information tha开发者_JS百科t I need on each page, I preload. However if something is needed from a rarely accessed table - then I do

$newdata = $db->Query('SELECT * FROM rare_table WHERE user_id='.$user->id);

I would like to simplify the above to a point where I don't have to specify that the query should be limited to this particular user. An ideal function call would be:

$newdata = $user->Query('SELECT * FROM rare_table');

Obviously I'd have to parse the SQL and add a WHERE clause. Or add to the already existing clause.

Questions: are there tools to do this? How can I develop this? Is this even a good idea?


Edit: Found an SQL parser that may assist those interested in doing this.


Looks like you want to add a function to user

public function getRow($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     //run query

The issue is getting $db, does it belong to user? do you have a function to get it?

public function getRowFrom($tableName)
{
     $sql = "SELECT * FROM $tableName WHERE user_id = " . $this->$id;
     $db = getDb(); //however you need to do this.  $this->db, DBTools::getDB(), call constructor here, etc.
     return $db->Query($sql);
}

and call it as

$user->getRowFrom('rareTableNo4');


I generally create a class called User and instantiate a singleton for each page (with the logged-in user.). Additional instances can be created for other users (e.g. $u2 = new User($name);) Then such things get embedded in User.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜