简体   繁体   English

使用CodeIgniters Active Record库来操作MySQL数据库或者我应该只使用SQL是一个好主意吗?

[英]Is it a good idea to use CodeIgniters Active Record library to manipulate MySQL databases or should I just use SQL?

I'm starting to get to grips with CodeIgniter and came across it's support for the Active Record pattern. 我开始掌握CodeIgniter,并且发现它支持Active Record模式。

I like the fact that it generates the SQL code for you so essentially you can retrieve, update and insert data in to a database without tying your application to a specific database engine. 我喜欢它为您生成SQL代码这一事实,因此基本上您可以检索,更新数据并将数据插入到数据库中,而无需将应用程序绑定到特定的数据库引擎。

It makes simple queries very simple but my concern is that it makes complex queries more complex if not impossible (eg if need for engine specific functions). 它使简单查询变得非常简单,但我担心的是,如果不是不可能的话,它会使复杂查询变得更复杂(例如,如果需要特定于引擎的函数)。

My Questions 我的问题

What is your opinion of this pattern especially regarding CodeIgniters implementation? 您对此模式有何看法,特别是有关CodeIgniters的实现?

Are there any speed issues with wrapping the database in another layer? 将数据库包装在另一层中是否存在速度问题?

Does it (logic) become messy when trying to build very complex queries? 在尝试构建非常复杂的查询时,它(逻辑)是否变得混乱?

Do the advantages out way the disadvantages? 优点是缺点吗?

Ok, First of all 99% of your queries will be simple select/insert/update/delete. 好的,首先99%的查询将是简单的选择/插入/更新/删除。 For this active record is great. 对于这个活跃的记录是伟大的。 It provides simple syntax that can be easily changed. 它提供了易于更改的简单语法。 For more complex queries you should just use the query method. 对于更复杂的查询,您应该只使用查询方法。 Thats what its for. 这就是它的用途。

Second, It provides escaping & security for those queries. 其次,它为这些查询提供了转义和安全性。 Face it, your application probably will have hundreds if not thousands of places where queries take place. 面对它,您的应用程序可能会有数百个(如果不是数千个)进行查询的地方。 Your bound to screw up and forget to properly escape some of them. 你必须搞砸,忘记妥善逃脱其中的一些。 Active record does not forget. 积极的记录不会忘记。

Third, performance in my experience is not dramatically affected. 第三,我的经验表现并没有受到太大影响。 Of course it is but its probably around .00001 per query. 当然,它可能是每个查询大约.00001。 I think that is perfectly acceptable for the added security and sanity checks it does for you. 我认为这对于它为您提供额外的安全性和健全性检查是完全可以接受的。

Lastly, I think its clear that i believe the advantages are far greater than the disadvantages. 最后,我认为很明显,我相信优势远远大于劣势。 Having secure queries that even your most junior developer can understand and not screw up is a great thing. 即使是最初级的开发人员也可以理解而不是搞砸了安全查询,这是件好事。

What is your opinion (sic) of this pattern especially regarding CodeIgniters implementation? 您对此模式有何看法(原文如此) ,特别是有关CodeIgniters的实现?

Can't say much about CI's implementation. 对CI的实施不能说太多。 Generally I avoid AR for anything but the simplest applications. 一般来说,除了最简单的应用程序外,我避免使用AR。 If the table does not match 1:1 to my business objects, I don't use AR, as it will make modeling the application difficult. 如果表与业务对象的1:1不匹配,我不使用AR,因为它会使应用程序建模变得困难。 I also don't like the idea of coupling the persistence layer to my business objects. 我也不喜欢将持久层耦合到业务对象的想法。 It's a violation of separation of concerns. 这违反了关注点的分离。 Why should a Product know how to save itself? 为什么产品应该知道如何自救? Futher reading: http://kore-nordmann.de/blog/why_active_record_sucks.html 更进一步阅读: http//kore-nordmann.de/blog/why_active_record_sucks.html

EDIT after the comment of @kemp, I looked at the CI User Guide to see how they implemented AR: 在@kemp的评论之后 编辑 ,我查看了CI用户指南以了解他们如何实现AR:

As you can see in PoEAA an AR is an object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. 正如您在PoEAA中看到的,AR是一个对象,它在数据库表或视图中包装行,封装数据库访问,并在该数据上添加域逻辑。 This is not what CI does though. 这不是CI所做的。 It just provides an API to build queries. 它只是提供了一个用于构建查询的API。 I understood that there is a Model class which extends AR and which can be used to build business objects, but that would be more like a Row Data Gateway then. 我知道有一个扩展AR的Model类,它可以用来构建业务对象,但那更像是一个Row Data Gateway Check out PHPActiveRecord for an alternate implementation. 查看PHPActiveRecord以获取替代实现。

Are there any speed issues with wrapping the database in another layer? 将数据库包装在另一层中是否存在速度问题?

Whenever you abstract or wrap something into something else, you can be sure this comes with a performance impact over doing it raw. 无论何时抽象或将某些内容包装到其他内容中,您都可以确定这会带来性能影响。 The question is, is it acceptable for your application. 问题是,您的申请是否可以接受。 The only way to find out is by benchmarking. 找出答案的唯一方法是通过基准测试。 Further Reading: https://stackoverflow.com/search?q=orm+slow 进一步阅读: https//stackoverflow.com/search?q =orm +slow

EDIT In case of CI's simple query building API, I'd assume the performance impact to be neglectable. 编辑在CI的简单查询构建API的情况下,我认为性能影响是可忽略的。 Assembling the queries will logically take some more time than just using passing a raw SQL string to the db adapter, but that should be microseconds only. 组装查询在逻辑上比仅使用将原始SQL字符串传递给数据库适配器需要更多的时间,但这应该只是微秒。 And you as far as I have seen it in the User Guide, you can also cache query strings. 就你在用户指南中看到的那样,你也可以缓存查询字符串。 But when in doubt, benchmark. 但当有疑问时,基准。

Does it (logic) become messy when trying to build very complex queries? 在尝试构建非常复杂的查询时,它(逻辑)是否变得混乱?

Depends on your queries. 取决于您的疑问。 I've seen pretty messy SQL queries. 我见过非常混乱的SQL查询。 Those don't get prettier when expressed through an OO interface. 当通过OO接口表达时,这些不会变得更漂亮。 Depending on the API, you might find queries you won't be able to express through it. 根据API,您可能会发现无法通过它表达的查询。 But then again, that depends on your queries. 但话又说回来,这取决于你的疑问。

Do the advantages out way the disadvantages? 优点是缺点吗?

That only you can decide. 只有你可以决定。 If it makes your life as a programmer easy, sure why not. 如果它让你的程序员生活变得轻松,那么为什么不呢。 If it fits your programming needs, yes. 如果它符合您的编程需求,是的。 Ruby on Rails is build heavily on that (AR) concept, so it can't be all that bad (although we could argue about this, too :)) Ruby on Rails主要依赖于(AR)概念,因此它不会那么糟糕(虽然我们也可以争论这个:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM