0

Have anyone seen an ORM for PHP, that does not require me to define everything at the development time?

For example, I would like "rating" field to be added to "User" object by "Rating" plugin (using hooks for instance). I would like validation rules for this new field also to be added by the plugin. Of course, plugin will also do ALTER TABLE to add a new field into "user" table in the database on installation.

ADDED: I don't need ORM's that 'guess' database structure in runtime (like DataMapper ORM). I need plugins (php scripts) to be able to add fields to objects.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • Hmm, I'd tend to recommend a key/value table for that, rather than let users add their own columns - could get messy. Or consider a single text column containing a serialised set of values - I believe Wordpress does that against its user table, to good effect. – halfer Mar 13 '12 at 14:23
  • @halfer That aren't users, who will add their own columns, but plugins for main script. For example, a plugin can be written for a e-commerce script, that count product views. With current ORMs, that plugin MUST have it's own table. I would like to see ORM, that would allow such plugin to add "viewed_times" field to the existing table. That, of course, leads to several other problems - like plugin should have hooks to add respected field to all forms, using product table etc. But I'm now just interested in an existance of such ORM – Vladislav Rastrusny Mar 14 '12 at 09:00
  • Sounds like you could write a custom Propel Behaviour for that - this could either maintain a 1:1 relationship with an invisibly created new table, or add extra fields onto an existing table. – halfer Mar 14 '12 at 10:50
  • Actually, scratch that idea - behaviours do _add new functionality_, but not at runtime. I would go back to the table modification suggestion in my answer below. – halfer Mar 14 '12 at 12:35
  • Doctrine2 can do it, see answers to similar questions on how: http://stackoverflow.com/a/4812580/295689 and http://stackoverflow.com/a/10451751/295689 – Maxym May 13 '13 at 11:17

3 Answers3

0

I know that datamapper orm for codeigniter uses 'DESCRIBE' to identify the table structure at run-time. Don't know if that would be any good for your project though.

Stevo
  • 2,601
  • 3
  • 24
  • 32
0

Maybe redbean, http://redbeanphp.com/, is something for you? I haven't tried it myself yet, but by the looks it seems to do what you want.

joakimbeng
  • 867
  • 7
  • 9
  • I think RB offers a 'fluid development' mode, in which tables are modified during dev time and are 'fixed' at run time. I thought of this too, but I am not sure it is quite what the OP wants - I suspect they want to modify tables at runtime. – halfer Mar 13 '12 at 14:30
  • @halfer Yes, that's exactly what I want. I need to provide plugins to integrate into existing models. Although, it might be an informal idea :) – Vladislav Rastrusny Mar 14 '12 at 12:06
0

Having added my earlier comment; if you are sure you want to modify core tables, you can use Propel. Have a look at my post here on how to generate run-time classes. I don't know how to do migrations however (which is what you'd need for the ALTER TABLE stuff) - but I believe it is supported.

Also - I am using a lot of Propel stuff at the moment to generate a database builder - can supply more code examples if you want. I discovered when writing this stuff that there aren't many examples of manipulating tables/models on the web, but Propel 1.x can definitely do it.

halfer
  • 19,824
  • 17
  • 99
  • 186