Brainstorm: Improving Data Handling in PHP

For the past few months I’ve been working on a data handling library for PHP. This library will completely change the way we access and handle data in PHP. It is meant to combine all the tasks we often repeatedly perform that are related to our data. The idea behind it is to give developers the ability to instantiate an object that contains all the information pertaining a set of data, and tools to manipulate it accordingly.

The main features/ideas for this library so far:

  • Create, read, update and delete records - as is expected from modern database abstraction layers. It only works with MySQL at the moment, I am planning on using the Adapter design pattern to make it work perfectly with other databases.
  • Validate data - At the moment, the validation system is a bunch of cases in a switch system, but I intend to make it much more powerful.
  • Filter, sanitize and escape data – so that you can be sure your data is always valid, and won’t break your website. At the moment, not much work has been put into this part, I’ve been working on data relationships, but I am thinking of making it very strict, allowing nothing by default, so that developers are compelled to display their data properly.
  • Analyze data - Be it simply counting all the rows in a set of data, finding out an average, or performing more advanced statistical analysis, I want it to be possible using this library.
  • Import/Export data - I want to be able to convert data between different formats and import or export that data into different formats. For example, I’d like to getLatest(10)->export(‘RSS’) and get an RSS file containing the 10 latest results in a data set (getLatest would know which field the creation date field is, and act accordingly). JSON, CSV and XLS are three more data formats that I’d like to support in the near future.
  • Generate the HTML – Necessary for accessing and manipulating the data, as well as repopulating the forms when validation fails, inserting default values, etc.. This has to be extremely flexible: it must be possible to integrate this into an existing theme/layout as easily as possible, and also to make custom layouts, in case the developer wants to.
  • Handle Data Relationships – This is extremely important to me. For example, if I had an instance of this library that had all the information about my users table, I would like to not only be able to handle the data on that table, but also get all the posts related to that user, or some additional user metadata that’s in another table (WordPress does that, for example). This is possible at the moment using custom fields (more below).
  • Custom Fields – Sometimes, you don’t just want to stick with the fields in your database table, you want more. For example, you might want to display a list of your users, and the number of purchases they have completed on your website. Or you might wanna get an array of IDs of posts that your user has written. With custom fields, you can – you tell the library where the data is, and it will take care of everything for you.
  • Field Identifiers – Field identifiers allow you to completely abstract your code from the underlying database structure. For example, let’s say you have two database tables, one with USERNAME and PASSWORD and the other with user_name and user_password. Or you had USERNAME and PASSWORD, but decided to change them to something else. By using field identifiers, it is possible for you to change the database fields, and the only thing you have to update is the configuration of your data set. The code remains the same.
  • Method Query Language (MQL) – While developing this library, I felt that I needed a way to make my querying even easier. I had found myself writing functions getById, getByUser, and others, over the years, and I decided to implement this into the library. The results is an astonishingly simple way to both query and update the database: getFieldsByFields() and setFieldsByFields(). An example would be getUsernameById($Id), to get a user’s username, given their ID, or setAddressAndZipcodeById($Id, $Address, $Zipcode) to update the address and zipcode of the user with a given ID. It’s that simple. And thanks to Field Identifiers, if your database structure changes, you still won’t have to change the function names, so it won’t be a problem.
  • REST-ful API – Just about every modern day website has to have an API to enable remote access to their data. The library includes an api/ folder, with everything necessary to make remote calls to data using method query language. By default, you can use: http://www.example.com/api/api-key/data-set/method-query/arguments. An example of this is http://api.example.com/api-key/articles/getLatest/3. This API can easily be customized to your needs, and by default it is impossible to access or use any field in the API, you have to enable API access for them manually. For example, you might want to allow API access to a list of your posts, their author’s, post creation date, and all of that, but not their metadata.

Those are the main things I’m working on at the moment. I’ll be discussing them further here while I develop them, and I would like to hear your feedback. I will also release the code under the MIT license very soon, maybe even set up a git repository or something, in case any of you wants to help me out developing this mega project.

It’s easy to use this library. Basically, you create a configuration file that contains the fields in a database table, their validation rules, permissions (whether or not you can edit that field in the HTML forms, whether or not you can access the field using the API), and all of the things pertaining to the database table. Once that’s done, all you need to do is instantiate the library, pass it that configuration file, and you can start using it straightaway. Simple.

This entry was posted in Personal Projects and tagged , , . Bookmark the permalink.