Common CRUD Functions in PHP

Is there an easy way to write a generic function for each of the CRUD (create, retreive, update, delete) operations CRUD (create, retreive, update, delete) in PHP WITHOUT using any framework. For example, I want to have one create function that takes table names and field names as parameters and inserts data into mySQL database . Another requirement is that the function must be able to support Ie connections; it must be able to insert data into multiple tables, if necessary.

I know that these tasks can be performed using the framework, but for various reasons - too long to explain here - I cannot use them.

+6
php crud
source share
8 answers

I wrote this, it's a kind of polished scaffold. This is basically a class whose constructor accepts the table to be used, an array containing the names and types of fields, and an action. Based on this action, the object calls the method on its own. For example:

This is the array I pass in:

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

Then I call the constructor:

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

In the above case, the constructor calls the "edit" method, which represents a form that displays data from the $ table, but only the fields that I set in my array. The record used is determined by the $ _GET method. In this example, the field β€œstudent” is represented as a text field (hence the type β€œtext”). "Title" is just a shortcut used. Being "hidden", the identifier field is not displayed for editing, but is available for use by the program.

If I passed 'delete' instead of 'edit', it would delete the entry from the GET variable. If I gave only the table name, then by default there would be a list of records with buttons for editing, deleting, and a new one.

This is just one class containing all CRUDs with great customizability. You can make it complex or simple as you wish. By making it a universal class, I can drop it into any project and simply pass instructions, table information, and configuration information. I might not want for one table to have new entries added through the scaffold, in which case I could set "newbutton" to false in my parameter array.

This is not a framework in the usual sense. Just a standalone class that handles everything internally. There are some disadvantages. The key should be that all my tables should have a primary key with the name "id", you can leave without this, but this will complicate the situation. Another creature is that a large array detailing information about each managed table needs to be prepared, but you only need to do this once.

For a tutorial on this idea see here

+1
source share

If you try to write such a function, you will soon find that you just understood one more infrastructure.

+7
source share

Of course not, why does this framework exist and implement complex objects. First, I tried to convince anyone to actually use the existing infrastructure, and secondly, not having received the above, I would look at one or two of them and copy the implementation ideas. Otherwise, you can take a look at http://www.phpobjectgenerator.com/

+3
source share

Without any frameworks without ORM? Otherwise, I would suggest looking at Doctrine or Propel .

+2
source share

I know how you feel.

Pork.DbObject is a simple class from which you can extend your objects. To work, you just need the db connection class.

please check: www.schizofreend.nl/pork.dbobject/

(oh yeah, yuk @php object generator. bloat alert! who wants to have these custom functions in every class ???)

+2
source share

I think that you should write your own functions that achieve CRUD, if you are not stressful at the time. it may be a framework, but you need to find out what the framework does before the framework screams ... It is also useful to know these things, because you can easily find errors in the framework and fix them yourself .......

+1
source share

I stumbled upon this question on SO a while ago, and I did not find anything at the time that made this easy.

I ended up writing my own, and recently I found to open it (MIT license) if others might prove to be useful. It's on Github, feel free to check it out and use it if it suits your needs!

https://github.com/ArthurD/php-crud-model-class

I hope it finds some application - I would like to see some improvements / contributions, so feel free to send requests for traction! :-)

+1
source share

it is possible, but I would not recommend it.

If there is absolutely no way to use the framework, you can create a base class that extends all other objects of the model. Then you can make the base class generate and execute SQL based on get_class() and get_class_vars() .

Is it possible? Yes.
I would recommend? No

0
source share

All Articles