数据输入抽象层?
我想要一个自定义函数来从数据库
中插入/编辑/删除
记录,而无需 必须为每个页面编写单个查询。
我想这样继续:
//page.php:
$datainput = new DataInputAbstraction();
$datainput->setTableName = 'portal';
$datainput->setAllowedFields = array('name'=>'defaultvalue','text'=>'','desc'=>'');
$datainput->run();
// DataInputAbstraction.class.php:
class DataInputAbstraction{
//> attr
public function run(){
if (isset($_POST['action']) && $_POST['action']=='insert') {
$filteredData = array_intersect_key($this->allowedFields,$_POST);
//> Do a mysql query to insert $filteredData to $this->table
}
//> Do other stuff if $_POST['action'] == 'edit' | 'del'
}
}
基本上方法 run() 基于 $_POST['action'] 执行正确的操作。
通过使用属性 allowedFileds 执行 intersect_key 来过滤不需要的变量,然后构建查询
INSERT INTO $this->tableName (array_keys($field)) VALUES (array_values($field)) (伪代码)
您认为这是一个好方法吗?有更好的方法或者 php 提供类似的内置功能吗?
谢谢
I would like to have a custom function to insert/edit/delete
records from a database
without
having to write the single queries for each page.
I have thought to proceed like this:
//page.php:
$datainput = new DataInputAbstraction();
$datainput->setTableName = 'portal';
$datainput->setAllowedFields = array('name'=>'defaultvalue','text'=>'','desc'=>'');
$datainput->run();
// DataInputAbstraction.class.php:
class DataInputAbstraction{
//> attr
public function run(){
if (isset($_POST['action']) && $_POST['action']=='insert') {
$filteredData = array_intersect_key($this->allowedFields,$_POST);
//> Do a mysql query to insert $filteredData to $this->table
}
//> Do other stuff if $_POST['action'] == 'edit' | 'del'
}
}
basically the method run() performs the right action based on $_POST['action'].
Filters unwanted variables present with doing an intersect_key with the attribute allowedFileds, after that it builds the query to
INSERT INTO $this->tableName (array_keys($field)) VALUES (array_values($field))
(pseudocode)
Do you think this is a good way to proceed? There are better way or does php offers a similar built-in functionality?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,如果你不想使用已经存在的东西(顺便说一句,RedBean 是一个非常好的选择),那么你最好继续这样做。
我总是喜欢尽可能多的抽象,因为它可以让您不必就使用什么服务做出艰难的决定。例如,如果您稍后更改数据库提供程序,您将创建一个数据抽象层来处理所有数据事务,唯一需要重新编码的是数据抽象类。
所以,我认为这是一个好主意。我唯一的建议是确保遵守 DRY 原则并保持抽象层非常模块化。使用抽象层的代码不应该知道它是如何工作的,只知道它提供了所需的数据。
希望有帮助。
In my opinion, if you don't want to use something that is already out there (RedBean is a really nice choice BTW), then you would be good to proceed about this.
I always prefer as much abstraction as possible because it keeps you from making hard decisions about what service to use. For example, you will create a data abstraction layer to handle all your data transactions that way if you change database providers later on the only thing you have to recode is your data abstraction class.
So, I think it's a good idea. My only recommendation would be to make sure you adhere to DRY principals and keep your abstraction layer very modular. The code that uses the abstraction layer shouldn't know anything about how it works, just that it provides the data needed.
Hope that helps.
它永远不会真正起作用,因为您必须设置不同的字段集以进行插入和更新。
然而,这个想法本身已经足够好,而且形式
也可以。
然而,我自己更喜欢简单的 SQL 而不是一些抽象
it will never work in real as you have to set up different field sets for insert and update.
However, the idea itself is good enough and in the form of
would be okay.
However, I myself prefer plain SQL over some abstractions