Multi-tier applications with PHP?

I am relatively new to PHP, but an experienced Java programmer in complex enterprise environments with SOA architecture and multi-tier applications. There we usually use business applications with middle tier business logic.

I am programming an alternative currency system that should be easily deployable and customizable by individuals and communities; it will be open source. This is why php / mysql seems to be the best choice for me.

Users have accounts and they receive balance. In addition, the system calculates prices based on the total volume of services provided and the total available assets.

This means that a series of settlements occurs upon purchase; balance and totals are updated; these are derived digits that usually do not fit in the database.

However, I resorted to installing triggers and stored procedures in the db, so none of those updates were made in the php code.

What do people think? Is this a good approach? My experience tells me that this is not the best solution, and suggests that I implement the middle level. However, I don't even know how to do it. On the other hand, what I still see in the store seems to me the most appropriate.

I hope I have put my question clear. All comments are appreciated. There can be no "perfect" solution.

+2


source to share


4 answers


As with these days, getting away from the DB is generally a good thing. You get lighter version control and you can only work in one language. Moreover, I believe stored procedures are the hard way to go. On the other hand, if you like this stuff and feel comfortable with SP in MySql, they are not bad, but I have always felt that they are more difficult to debug and complicate.

In the question of triggers, I'm not sure if this is necessary for your application. Since the events triggering computations are invoked by the user, these things can occur in PHP even if the user is redirected to the wait page or another page. Obviously, true triggers can only be executed at the DB level, but you can use a daemon thread that runs the PHP script every X seconds ... Avoid this at all costs and try to trigger an event to fire from the user side.



All that said, I wanted to plug in my favorite PHP data access layer solution: Doctrine . It's not perfect, but PHP is what it is, it's good enough. Does most of what you want and keeps you working with objects instead of database procedures etc.

As far as your heading is concerned, several levels in PHP are completely doable, but you must follow them and respect them. PHP code can call other PHP code and now it's (5.2+) nicely OO and all. Make sure to ignore the fact that a lot of PHP code you'll see around is complete crap and doesn't even use methods, let alone levels and decent OO modeling. All of this is possible if you want to do this, including doing your own (or using an existing) MVC solution.

+1


source


One of the problems with pushing a lot of features to the DB layer rather than the data abstraction layer is that you end up in the DBMS feature set. Open source software is often written in such a way that it can be used with different databases (of course, not always). It may be that along the way you want to simplify porting to postgres or some other DBMS. Using a lot of MySQL specific functions will now make it harder.



+1


source


There is absolutely nothing wrong with using triggers and stored procedures and other functions provided by your db server. It works and works well, you are using the full potential of the DB, instead of just dumping it into a lightweight data store.

However, I'm sure that for every developer who agrees with you (and me), there are at least as many who think of the exact opposite and have good experience with it.

0


source


Thanks guys.

I used db triggers because I thought it would be easier to monitor the integrity of transactions. As you can imagine, I am a developer who is also trying to access db knowledge.

Now I see that there is a solution to distribute PHP code at multiple levels, not only logically, but also physically, deploying to different servers.

However, at this point in development, I think I'll stick with my trigger / sp solution as it doesn't seem to be wrong. Spreading across multiple levels would require me to constantly change my application.

Also, thinking that open source, if anyone likes an alternative monetary system, it might be easier for people to just change the layout to their requirements, while I don't have to worry about the calculations being wrong if people touch php -code.

On the other hand, of course I agree that a db file can be very difficult to debug.

The DB init scripts are in source control, as are the php files :)

Thanks again

0


source







All Articles