数据包装方法
我正在使用 PHP 和 MySQL 进行开发。
有关当前登录用户的信息存储在许多不同的表中。我预先加载了每个页面上所需的信息。但是,如果需要从很少访问的表中获取某些内容,那么我
$newdata = $db->Query('SELECT * FROM rare_table WHERE user_id='.$user->id);
想将上述内容简化到不必指定查询应仅限于该特定用户的程度。理想的函数调用是:
$newdata = $user->Query('SELECT * FROM rare_table');
显然我必须解析 SQL 并添加 WHERE 子句。或者添加到已经存在的条款中。
问题:有工具可以做到这一点吗?我该如何开发这个?这是个好主意吗?
编辑:找到SQL 解析器 可以帮助那些对此感兴趣的人。
I'm developing in PHP and MySQL.
The information about the currently logged in user is stored in many different tables. The information that 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想向用户添加一个功能
问题是获取 $db,它属于用户吗?你有一个函数可以得到它吗?
并将其称为
Looks like you want to add a function to user
The issue is getting $db, does it belong to user? do you have a function to get it?
and call it as
我通常创建一个名为 User 的类,并为每个页面实例化一个单例(使用登录用户)。可以为其他用户创建额外的实例(例如 $u2 = new User($name);),然后这些东西就会嵌入到 User 中。
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.