Simple / reusable CRUD in PHP (no cool work or big classes)

I am working on a PHP CMS project and I am trying to figure out what is the most convenient way to work with the CRUD functionality in PHP.

The CMS is fully programmed in procedural PHP (no OOP - I know many of you will disagree with that ...) and has been designed to be as simple and light as possible, while also creating reusable functions and code snippets.

CMS allows you to install or activate multiple modules based on your needs. These modules describe different types of content, so I probably end up with something like pages, news, blogs, to name a few.

For each of these content types, I will have to create CRUD operations, and now I am trying to find the most convenient way to do this.

One requirement would be that the form for each of these content types is contained in a single external file (for both insertion and editing), and if there is some way to integrate server side validation that would be a plus.

+1


source to share


4 answers


By CRUD operations, you only mean (tedious) database queries?

You can just as easily set up your database so that, with the exception of a few common fields among content types, all data for a particular content type is stored as a serialized associative array in a TEXT field.

Thus, you only need one set of CRUD queries for any particular content type, since the data passed to the CRUD function is simply blind serialized.

For example, we declare that content title, created / updated date, tags and short description are considered general data. From there we have a blog and a page content type.

I could create a database table as such:

CREATE TABLE `content` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR  NOT NULL,
  `short_description` TEXT  NOT NULL,
  `tags` TEXT ,
  `data` TEXT ,
  `content_type` INT  NOT NULL,
  `created_at` DATETIME  NOT NULL,
  `updated_at` DATETIME  NOT NULL,
  PRIMARY KEY (`id`)
)

      



(Go ahead and assume we create reference tables for content_type)

And while a blog might need data such as "pingbacks" and the page might only need a body, you just save the output of something like the following example for a blog:

$data = serialize(array(
    "body" => "Lorem ipsum",
    "pingbacks" => array()
));

      

Updates are simple, when you grab data from a database, you unserialize the data for editing in a form selected based on content type. The mapping works the same, just take a template based on the content type and send an unserialized dataset to it. The template should never have to worry about how the data is stored, it just gets $ data ['pingbacks'].

As for your forms, my suggestion is to break your anti-OOP precept and find a form generation library. If you can fetch it from the framework, Zend_Form with Zend_Config and Zend_Validate from Zend Framework(all Zend_Config in this situation is a convenient interface for loading and moving XML and INI files) makes life very enjoyable. You can have your XMLs define a form for each content type, and all you could do is just render the form on your page (grab the XML based on the content type), grab the filtered data, remove the "common fields" e.g. name created / updated dates and then serialize what's left in the database. No schema knowledge is required for a specific content type (unless you want to be strict).

Even though I'm personally on the sidelines, I would strongly suggest that you take a look at capturing Zend_Form (with Zend_Validate and Zend_Config) and also using Doctrine as an ORM / database abstraction layer. You may find that at least Doctrine will make your life a lot easier when it comes to performing database operations.

+4


source


While I am on the personal side, I would strongly suggest that you look into capturing Zend_Form (with Zend_Validate and Zend_Config) and using Doctrine as your ORM / database abstraction layer. You may find that at least Doctrine will make your life a lot easier when it comes to performing database operations.

I agree with dcousineau. Why roll back when it's already done? I would also look at Zend DB and if you need a PHP4 and 5 PHP ADOdb solution .



I recently started an academic project and had the same desire as you; I ended up going with PHP ADoDB.

+1


source


I suggest www.ajaxcrud.com - it's easy to use, lightweight and gets you up and running in a matter of seconds.

0


source


You can try this: http://xcrud.com , very helpful

0


source







All Articles