tags:

views:

4680

answers:

8

Hi,

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.

many thanks in advance.

Vinayak

+2  A: 

Without any frameworks includes without any ORMs? Otherwise I would suggest to have a look at Doctrine or Propel.

unexist
+1  A: 

Of course not, that's why those frameworks exist and implement crud facilities. I'd first try to convince whomever it takes to actually use an existing framework and second, failing the above, I'd take a look at one or two of them and copy the implementation ideas. Failing all that you could take a look at http://www.phpobjectgenerator.com/

Vinko Vrsalovic
A: 

it is possible but I wouldn't recommend it.

If there's absolutely NO way to use a framework you could create a base class that all other model objects extend. You can then make the base class generate & execute SQL based on get_class() and get_class_vars().

Is it possible? Yes.
Would I recommend it? nope

arin sarkissian
+3  A: 

If you try to write such function you'll soon discover that you've just realized yet another framework.

Sergei Stolyarov
+1  A: 

I know the way you feel.

Pork.DbObject is a simple class that you can extend your objects from. It just needs a db connection class to work.

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

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

SchizoDuckie
+1 for the neatness of Pork.DbObject!
Alix Axel
A: 

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:

This is the array I pass:

$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 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.

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.

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.

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: http://www.shadow-fox.net/site/tutorial/39-Creating-A-Scaffold-like-Class-in-PHP-or-An-Automatic-CMS-For-a-Table

lewis
Firefox doesn't like your website.
Alix Axel
this website seems to have been down for a while? I've tried a few times...
matt
A: 

I think you should write your own functions that achieve CRUD unless you are stressed for time. it might be a framework on it's own but you need to learn what the framework does before screaming framework....it also becomes handy to know these things because you can easily pickup bugs on the framework and fix them your self........

Ronald Conco
A: 

Here i’ve made a self-written PHP / Ajax Datagrid component using JQGrid (based on JQuery). This library can generate fully featured CRUD application in record time.

include("jqgrid.php"); $g = new jqgrid(); $grid["caption"] = "My Sample Grid"; // set grid customizable params $g->set_options($grid); $g->table = "tags"; // db table for CRUD operations.

// You can also specify SQL query for displaying data (download code with examples) // $g->select_command = "select f1,f2,f3 from tags";

$out = $g->render("my_grid_1"); // render grid

... and in HTML after including JS files ...

This code will result in fully functional Jquery Grid (JqGrid) with …

  • Add
  • Edit
  • Delete
  • Search
  • Auto-filter
  • Sort
  • Pagination
  • Export
  • Multiple Themes (ThemeRoller)
  • and almost all features available for FREE

http://azgtech.wordpress.com/2010/08/01/jqgrid-php-datagrid-component-free/

Jqgrid php