PHP 数据库抽象:定义模型一次,生成代码,但处理纯数据而不是活动记录对象
我正在寻找未来 PHP 项目中的数据库抽象层。到目前为止,我一方面使用普通的 SQL 命令和数组,另一方面使用像 Doctrine 这样功能齐全的 ORM。
我不喜欢 ORM,因为大多数时候工作是从数据库获取数据列表并将其发送到模板引擎。两者都可以很好地处理数组,并且当模板只需要普通数组时,无需通过将数据封装在对象中来使一切变得复杂。我也不喜欢纯 SQL 方法,因为对于简单的 CRUD 内容,我必须自己编写所有内容,而且我也不喜欢维护数据的两种模型(一个在数据库中,另一个在我的数据库访问对象中) )。
有没有什么东西可以让我只定义一次模型并为我生成基本的东西(就像在 Doctrine 中一样),但又可以让我摆脱活动记录模式?
I am searching for a database abstraction layer in future PHP projects. Until now I have worked with plain SQL-commands and arrays on the one hand fully featured ORMs like Doctrine on the other hand.
I don't like ORM because most of the time the job is to get a list of data from the database and to send it to the template engine. Both work fully fine with arrays and there's no need to make it all complicated by capsule the data in an object when the template just needs the plain array. I don't like the plain-SQL approach either because for the simple CRUD stuff I have to write everything myself and also I don't like to maintain two models of my data (one in the DB and the other within my database access object).
Is there something which let's me define the model one time only and generates the elementary stuff for me (like in Doctrine) but which saves me from the active record pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库不是模型,它是原始数据源,而模型理论上不仅仅是数据字段,还应该是影响模型数据的方法。
即使您使用封装 sql 的库或者您自己编写它,您始终必须告诉程序要插入什么内容以及插入到数据库的何处。
如果您发现自己一遍又一遍地编写类似的“插入”和“选择”命令,那么是时候设计一些函数或类了。
The db is not a model, its the raw data source, and the model is, in thoery, more than just the data fields, it should also be the methods that affect the model's data.
You are always going to have to tell the program what to insert and where into the db, even if you use a library that encapsulates the sql or if you write it yourself.
If you find yourself writing the similar "insert" and "select" commands over and over, then its time to design some functions or classes.