PHP中常见的CRUD函数 [英] Common CRUD functions in PHP

查看:156
本文介绍了PHP中常见的CRUD函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在不使用任何框架的情况下为PHP中的每个CRUD (create, retreive, update, delete)操作编写通用函数.例如,我希望有一个将表名和字段名作为参数并将数据插入mySQL database的create函数.另一个要求是该功能应该能够支持联接,即如果需要,它应该能够将数据插入到多个表中.

Is there a simple way to write a common function for each of the CRUD (create, retreive, update, delete) operations in PHP WITHOUT using any framework. For example I wish to have a single create function that takes the table name and field names as parameters and inserts data into a mySQL database. Another requirement is that the function should be able to support joins I.e. it should be able to insert data into multiple tables if required.

我知道可以通过使用框架来完成这些任务,但是由于种种原因-冗长的解释-我无法使用它们.

I know that these tasks could be done by using a framework but because of various reasons - too lengthy to explain here - I cannot use them.

推荐答案

我写的正是这本书,它是一种抛光的脚手架.基本上,这是一个类,其构造函数使用要使用的表,包含字段名称和类型的数组以及一个操作.基于此操作,对象将自行调用方法.例如:

I wrote this very thing, it's kind of a polished scaffold. It's basically a class the constructor of which takes the table to be used, an array containing field names and types, and an action. Based on this action the object calls a method on itself. For example:

这是我传递的数组:

$data = array(array('name' => 'id', 'type' => 'hidden')
          , array('name' => 'student', 'type' => 'text', 'title' => 'Student'));

然后我调用构造函数:

new MyScaffold($table, 'edit', $data, $_GET['id']);

在上述情况下,构造函数调用"edit"方法,该方法呈现一种形式,显示来自$ table的数据,但仅显示我在数组中设置的字段.它使用的记录由$ _GET方法确定.在此示例中,学生"字段显示为文本框(因此为文本"类型). 标题"只是所使用的标签.被隐藏"时,ID字段不会显示出来供编辑,但可供程序使用.

In the above case the constructor calls the 'edit' method which presents a form displaying data from the $table, but only fields I set up in my array. The record it uses is determined by the $_GET method. In this example the 'student' field is presented as a text-box (hence the 'text' type). The 'title' is simply the label used. Being 'hidden' the ID field is not shown for editing but is available to the program for use.

如果我传递的是删除"而不是编辑",它将从GET变量中删除记录.如果我仅传递一个表名,它将默认为带有用于编辑,删除和新建按钮的记录列表.

If I had passed 'delete' instead of 'edit' it would delete the record from the GET variable. If I passed only a table name it would default to a list of records with buttons for edit, delete, and new.

这只是一个包含所有CRUD并具有很多可自定义性的类.您可以根据需要将其设置为复杂或简单.通过使其成为通用类,我可以将其放入任何项目中,并仅传递指令,表信息和配置信息.我可能对于一个表不想允许通过支架添加新记录,在这种情况下,我可能会将参数数组中的"newbutton"设置为false.

It's just one class that contains all the CRUD with lots of customisability. You can make it as complicated or as simple as you wish. By making it a generic class I can drop it in to any project and just pass instructions, table information and configuration information. I might for one table not want to permit new records from being added through the scaffold, in this case I might set "newbutton" to be false in my parameters array.

这不是传统意义上的框架.只是一个内部处理所有事情的独立类.这有一些缺点.关键必须是我所有的表都必须有一个名为'id'的主键,您可以不用这个主键就可以逃脱,但这会使事情变得复杂.另一个原因是必须准备一个大型数组,其中包含有关要管理的每个表的详细信息,但是您只需要执行一次即可.

It's not a framework in the conventional sense. Just a standalone class that handles everything internally. There are some drawbacks to this. The key ones must be that all my tables must have a primary key called 'id', you could get away without this but it would complicate matters. Another being that a large array detailing information about each table to be managed must be prepared, but you need only do this once.

有关此想法的教程,请参见

For a tutorial on this idea see here

这篇关于PHP中常见的CRUD函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆