数据输入抽象层?

发布于 2024-10-26 11:31:31 字数 1002 浏览 2 评论 0原文

我想要一个自定义函数来从数据库插入/编辑/删除记录,而无需 必须为每个页面编写单个查询。

我想这样继续:

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-11-02 11:31:31

在我看来,如果你不想使用已经存在的东西(顺便说一句,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.

ぶ宁プ宁ぶ 2024-11-02 11:31:31

它永远不会真正起作用,因为您必须设置不同的字段集以进行插入和更新。

然而,这个想法本身已经足够好,而且形式

$datainput = new DataInputAbstraction();
$datainput->setTableName = 'portal';

if ($action=='insert'){
    $datainput->setAllowedFields = array('name','text','desc','timestamp');
} elseif ($action=='update'){
    $datainput->setAllowedFields = array('name','text','desc');
    $datainput->setWhereConditions = array('id' => $_POST['id']);
}
$datainput->run($action);

也可以。

然而,我自己更喜欢简单的 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

$datainput = new DataInputAbstraction();
$datainput->setTableName = 'portal';

if ($action=='insert'){
    $datainput->setAllowedFields = array('name','text','desc','timestamp');
} elseif ($action=='update'){
    $datainput->setAllowedFields = array('name','text','desc');
    $datainput->setWhereConditions = array('id' => $_POST['id']);
}
$datainput->run($action);

would be okay.

However, I myself prefer plain SQL over some abstractions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文