views:

86

answers:

1

I'm studying DBIx classes and I'm a bit confused since my interaction with a database so far has been plain SQL queries in PHP code.

Anyway, as I understand it, the class operates with the schema defined in the result classes instead of interacting directly with the database. The schema can be either be built manually via the various .pm classes or pulled from the database via the Loader class.

My question is: what is the preferred approach to this? What if I manually build my result classes and then alter my database? I guess I have to edit both the tables and the classes, isn't it a bit impractical?

Thanks

+1  A: 

One of the difficulties in using an object-relational mapping system like DBIx::Class is that you have to keep your table classes up-to-date with your schema. Usually, it's as simple as adding a field here and there, maybe adding a new foreign key relation once in a while. If you've got your code in version control, then it should be relatively simple to keep track.

The disadvantage of maintaining the same thing (DB schema) in two places is outweighed (IMHO) by the advantage of having an ORM system in place. Once you get used to working with DBIx::Class queries and their ResultSets things go a lot faster than writing raw DBI calls all the time.

Personally, I've not had good luck with the Loader classes, but it's been quite a while since I've tried them. If they work for you, it's probably easier than manually maintaining your schema definitions in the code. On the other hand, I have never been bothered by the minuscule effort that this requires. Database schemas tend not to change too often once a system is up and running.

friedo
Actually I'm not really bothered by the need to reflect any change I might do to in both places, I just wanted to know if that were the common approach. In fact it seems like a very little effort compared to the convenience of an organized schema vs SQL queries mixed with code. Thanks for your input.
kemp