开发者

Creating a Simple "ORM/ActiveRecord" Pattern

I am not sure what I'm doing is called ORM or Active Record Pattern.

I have an Entity base class that entities/database tables will inherit. These classes will have methods like

  • find
  • findBy
  • findAllBy
  • insert
  • update
  • delete
  • Getters & Setters for column data (eg. name, title, etc) via magic methods

Problem now is how do I create a database connection?

  • Dependency Injection - sounds complicated ...
  • Use a global variable, that these classes will expect to be set? - Doesnt sound right
  • Have a base class that Entity inherit that contain all database connec开发者_JAVA百科tion info? - doesnt sound right either

Maybe I am doing it wrong? I am open to any ideas, preferably simple for a start. I am wanting to create a simple framework for a start (not using Doctrine for example), it will give me a foundation on how such framework works. Also if its a small project, using a big framework may over-complicates things


There isnt really much thats simple about what youre attempting. Its complex thing :-)

You need to have some kind of basic entity manager and/or table class which usually holds the reference to the DB connection (or some sort of object that wraps it). All the Entity's then pass themselves to the manager when their save or delete methods are called and the manager will work out the query needed to modify the db.

You can either inject this manager, or make it a singleton and have your classes fetch it when instantiated for example.

If i were you i would check out PHP Objects Patterns and Practices by Matt Zandstra. It goes into all these patterns with some basic implementation examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜