17

I'm looking for Ruby's Active record for PHP. Something that is so simple that I just define my fields, extend the base ORM class, and I get ACID operations for free. I should get default getters and setters without writing any code, but overriding a default getter or setter is as easy as declaring get$fieldName or set$fieldName functions with the behavior I want. Symphony makes you create about 5 files per object, and all defined objects always load as far as I can tell. What is a better alternative? Why is it better? Can you put simple examples in your answers please?

Doctrine is another ORM I've looked at besides symphony . There also you need to create yaml files that describe your data structures. The database already defines this stuff. What will just read my table defs without having to generate and store config files everywhere?

Zak
  • 24,947
  • 11
  • 38
  • 68
  • I posted below but there's a solution to your Doctrine problems. Running a ./doctrine generate-models-db or ./doctrine generate-yaml-db reverse engineers your database into class and yaml files respectively. – dcousineau Oct 21 '08 at 22:51

13 Answers13

11

I'm a big fan of Doctrine which is a full featured ORM that will be replacing Propel as Symfony's default ORM.

It's got your basic ORM stuff you'd expect along with a full featured query builder that I've found to be wonderful.

It comes with a full suite of command line tools to manage your databases. For example, you can create your schemas and fixtures in YAML, have Doctrine generate classes based on your Schema, create the database, create the schema based on the models, then populate the database with your fixtures all with a single ./doctrine build-all-reload.

It also includes support for database migrations and recently updated the migrations to automatically diff and generate your migration models.

As per your doctrine complaints, you can run a command ./doctrine generate-models-db or ./doctrine generate-yaml-db to automatically create models and yaml files respectively from your current database setup.

Other niceties include "Behaviors" which makes life much easier when implementing certain, well, behaviors in your schema. For example you can add the "Timestampable" behavior to your class file. Doctine automatically adds a 'created_at' and 'updated_at' column, populates them, and every $object->save() you run automatically updates the 'updated_at' column. More complex behaviors include i18n, table versioning, and trees (though really only NestedSet).

Personally I've been extremely enamored with Doctrine and rave about it every chance I get.

dcousineau
  • 2,202
  • 18
  • 24
9

I use a little known orm layer called redbean. you can find it here : http://www.redbeanphp.com. its absolutely unique in the sense that it just creates tables columns and indexes all by itself without any configuration files at all. I find it to be a huge timesaver!

6

Both CodeIgniter (http://codeigniter.com/user_guide/database/active_record.html) and its PHP5 only fork Kohana (http://docs.kohanaphp.com/libraries/orm) contain implementations of the ActiveRecord pattern.

jakber
  • 3,549
  • 20
  • 20
  • Kohana seems to be the thing I am looking for. Zend's active record doesn't do any interdependencies, and in fact just gives you arrays back, not classes that you can operate on, or extend the functionality of. So Kahana looks to be the winner so far. – Zak Oct 21 '08 at 23:07
  • Zak, that's not true. Zend's Table methods return objects of type Zend_Db_Table_Row or Zend_Db_Table_Rowset. You can extend these classes. You can declare dependencies to other tables in your Zend_Db_Table classes. But whatever -- I'm sure Kahana will be adequate for your needs. – Bill Karwin Oct 22 '08 at 22:35
  • Thanks for the update. I've confirmed you're right on the Zend stuff. (Like I ever doubted ;) – Zak Mar 30 '09 at 19:09
5

I've created my own, without the bloat. (Though i need to update my on-site sources)

I created it with exactly the points in mind you mention: no dozens of xml files, no huge framework, just simple constructors with database to property mappigns and it does your basic CRUD / Find / Join stuff. For most of the stuff i do, i don't even need to write custom queries.

I've written all of this before on my site also, make sure to check out the basic examples to get the idea of it.

The next version i'll release comes with working one-line join on join on join (to walk a 'path' through your database), ini based database settings, cross database support, super-simple database abstraction and a standard logger that falls back to SQLite if your database is down.

Just give a shout if you're interested in the updates, i'll put a rush to it then.

Oh yeah and don't forget, there's also nice visual scaffold generator called Pork.Generator. It tries to analyze your database structure and find 1:1 1:many and many:many relations, and can automatically generate the classes for you :-) relations found in database
(source: schizofreend.nl)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SchizoDuckie
  • 9,353
  • 6
  • 33
  • 40
4

Zend_Db_Table and Zend_Db_Table_Row are fairly good at what you're describing. You don't need any configuration file, most metadata is "discovered" from the database itself.

Technically these classes don't implement the ActiveRecord pattern. Instead, they implement the Table Data Gateway and Row Data Gateway patterns. Together, these offer similar value as ActiveRecord, and in some ways are more flexible than ActiveRecord.

But as with any ORM, there are inevitably some SQL queries and operations that you can't do through the OO interface. No ORM can serve as one-stop shopping.

Footnote: I worked on the Zend Framework project for a little over a year, especially on the Zend_Db component. But I don't work for them anymore.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
1

Another option that follows Ruby DataMapper's implementation is phpDataMapper. It's obviously a Data Mapper instead of an ActiveRecord :).

Vance Lucas
  • 2,798
  • 1
  • 25
  • 21
1

I would recommend Doctrine with Symfony. Eventhough there is more to learn you will find it has the features you will need once the project grows (CRUD, Form framework, Record Templates, DQL, Plugin support, Behaviours). Both projects have very active community and you shouldn't find yourself in a dead end, because most of your questions have been already answered in official tutorials or in a forum.

If you don't like database definitions in YAML, you can always use ORM Designer or MySQL Workbench.

1

Check Maintainable framework. Although I prefer code generation over ActiveRecord (runtime reflection), I found Maintainable framework easy to use especially in terms of ORM features.

http://framework.maintainable.com/mvc/3_model.php#c3.7

If you want a framework based on code generation, try QCodo. Whatever dcousineau said for Doctrine, I can say for Qcodo. This is an event driven full-fledged framework imitating .NET/Delphi. However you can just code generation feature and find ways to dissociate your generated classed from the rest of the framework. Thus, you can embed generated classed within other frameworks.

0

http://dbphp.net

Pros

  • Generates/Modifies databases/tables/fields/various table/field attributes on the fly.
  • Needs no installation.
  • Has no config at all.
  • Needs only to include library and specify db link parameters to start work.
  • It has built in localization support.
  • It has various cache levels and allows to extend cache engine.
  • It works with many various database connections same time.
  • It can make relations between tables located in other database servers.
  • It extracts table/field attributes using class and variable doc comments.

Cons

  • It works only with objects. i.e. you must have defined class and have instance of class to save load or etc.
  • It has no site but has examples folder.
BIOHAZARD
  • 1,937
  • 20
  • 23
0

I like Idiorm and Paris and use them both on tiny projects. Idiorm is the actual ORM, whereas Paris is the active record implementation. You can use either one you'd prefer.

There is a also a fork called Granada, which is built over Idiorm and Paris, and adds support like eager loading etc.

Arda
  • 6,756
  • 3
  • 47
  • 67
0

Object relational mapper (ORM) for PHP that sits on top of a powerful database abstraction layer (DBAL). One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL. This provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication.

0

If you want an "on the fly" DB modelisation, where tables auto-fitting with data you push from code, take a look at FoxORM for DataMapper approach or RedBean for ActiveRecord approach

DevTheJo
  • 2,179
  • 2
  • 21
  • 25
0

I recommend QCubed. It's an incredibly powerful PHP5-only ORM framework that focuses on code generation, UI scaffolding, and rapid application development. Take a look at the training videos: http://qcu.be/content/video-screencasts

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59