You just need ORM or DBAL for an existing PHP application

I am working on an extension of an existing PHP application. Unfortunately for me the existing application is a mess. This is all spaghetti code with raw mysql_ * calls. Moan. I am not going to do it in the parts that I produce.

So I'm looking for a simple ORM DBAL that I can easily pop in and start using. Desired features:

  • It must work with an existing database schema. Preferably with little or no additional configuration. The existing database schema is of the same quality as the existing PHP code (no reasonable naming conventions, not normalized, etc.). I do not want to spend days converting a database schema manually into the properties of annotated objects a la Doctrine 2.
  • It should be able to work with existing raw mysql_ * queries. I don’t know how hydrating ORMs like Doctrine 2 or Propel behave when scripts manually manipulate the data in the database behind their backs, but I guess this is not very.
  • It should work on PHP 5.2.x. I would like to use PHP 5.3, but I don’t have the interest in looking at the existing 125K lines of spaghetti code to make sure it works on PHP 5.3.
  • No relationship required. In several places I need to get relational data, I will be happy to call additional find() or query() or something else.
  • Bonus points if they have trigger support (e.g. beforeSave , afterSave ). Not a requirement, but just nice to have.

Change Someone led me out of my misery. I just found out that 125K lines of spaghetti code also changes the database schema. For example, add an additional option somewhere, and an integer number of ALTER TABLE statements will fly. I could probably populate TheDailyWTF year with this codebase. So, one more requirement:

  • It should be able to automatically cope with a changing database schema (for example, add columns).

I have considered several solutions, but I am not sure how much they will work according to requirements. Doctrine 2, RedBeanPhp and the like all require PHP 5.3, so they are missing. There's an outdated version of RedBeanPhp for PHP 5.2.x, but I don't know if it will work with a dirty existing database schema. NotORM looks fine for receiving data, but I don’t know if it can be configured for an existing database schema and how you can easily return the data to the database.

Ideally, I would like something simple. For example:

 $user = User::find($id); $user->name = 'John Woo'; $user->save(); 

Or:

 $articles = ORM::find('article')->where('date' => '2010-01-01'); foreach ($articles as $article) { echo $article->name; } 

Any tips or even alternative solutions are welcome!

+6
php orm dbal notorm
source share
2 answers

I use ... http://github.com/j4mie/idiorm/

he also has an active record implementation in the form of Paris.

As for your editing. Idiorm deals with changing schemas, and the syntax almost exactly matches the type you want in your question.

+10
source share

How well have you studied the Doctrine? I am using Doctrine 1.2 for this kind of thing. A fairly simple setup allows you to start with an existing circuit. It automatically detects relationships between tables with foreign key constraints.

It has extensive trigger and behavior support, so bonus points can also be spent and they have relational support, so your extra queries are not needed. It has an excellent lazy load, and it has a flexible query language (called DQL) that allows you to do almost the same thing that you can do in SQL, only in terms of effort.

Your example would look like this:

 /* To just find one user */ $user = Doctrine::getTable('User')->findOneById($id); /* Alternative - illustrating DQL */ $user = Doctrine_Query::create() ->from('User u') ->where('u.id = ?',array($id)) ->fetchOne(); $user->name = 'John Woo'; $user->save(); 

It should be able to work with existing raw mysql_ * queries. I don’t know how hydrating ORMs such as Doctrine 2 or Propel behave when scripts manually manipulate data in the database behind their backs, but I guess this is not very.

Well, technically impossible to automate control; The SQL database simply does not drop material to your ORM, so to update material that has been changed in the background, you need to run an additional query anyway. Fortunately, Doctrine makes this very easy for you:

 /* @var User $user */ /* Change a user using some raw mysql queries in my spaghetti function */ $this->feedSpaghetti($user->id); /* Reload changes from database */ $user->refresh(); 
+1
source share

All Articles