mysql - implementing my first PHP model -
i've written small restful php backend using slim framework (http://www.slimframework.com/) interfaces mysql database, , right have 1 class doing db interactions , it's getting kinda big. it's time organize little more cleanly.
so based on understand mvc, better way might implement model layer so:
- each logical entity in system implemented data class. i.e. user accounts: class called "account" getid(), getname(), getemail(), etc etc
- and corresponding factory objects, i.e. accountfactory owns db connection , creates account class manipulate elsewhere in business logic layer.
- the business logic layer still pretty simple, maybe class called myapplication instantiates factories , uses them respond restful api calls.
- business logic might be, example, matching 2 accounts based on geographical location. in case, testing on data in 2 separate account objects instead of raw data loaded database.
but seems lot of refactoring time spent same thing. why wouldn't want use plain array data load database? it's not db-independent, sure, don't plan on switching away mysql @ moment anyway.
am approaching in correct way?
well, partly.
the first point describes model - m in mvc. abstracting "business logic" model makes sense in many ways. 1 use case website interacts same data rest api. reuse model , need build new controllers.
the "business logic"/"layer" controller - c in mvc. not give factory objects ownership of db connection, use cases may want use multiple factory objects should use same database connection...
i suggest read more structure , pro's , con's of mvc approach.
Comments
Post a Comment