codeigniter实例化的数据库对象的类型是什么?
我对 CodeIgniter 相当陌生,并且正在尝试找出一些我认为应该很容易但实际上并不容易的事情。
我的应用程序对 Product 对象做了一些处理,目前我只是通过 CodeIgniter 的 Database 类中包含的 ActiveRecord 功能来执行此操作。我通常更喜欢 ORM 而不是 ActiveRecord,以便正确地将业务逻辑与数据库逻辑分开。但在这种情况下,使用 Products 的对象通常会自行处理所有业务逻辑,因此我认为分离仍然有效。
我的 Product 对象通过 store() 方法与数据库交互,我希望该方法接受数据库连接作为参数。 store()
方法首先查询数据库以确定相关产品是否已以某种形式存在。如果是,则用新信息更新产品,如果不是,则插入产品。简单的东西。问题是,我想对 store()
方法参数使用类型提示,这样我就可以确保没有任何不具备基本 CodeIgniter 数据库/ActiveRecord 功能的对象会被传递到该方法中,而无需使用提出适当的错误。
对于我的一生,我无法弄清楚 CodeIgniter 数据库类的类型。在阅读了一些源代码之后,我想了一会儿 CI_DB_driver 类就是这个类。但是,当我使用它作为类型提示时,我的 IDE(确切地说是 NetBeans 7.01)无法识别 ActiveRecord 函数,也无法为我提供任何代码完成选项,例如 insert()
或 get_where()。我不认为我的 IDE 在这方面有问题,因为它确实可以识别
query()
或 insertString()
等方法。
我可以使用标准的 CodeIgniter 方式访问/连接数据库,即使用内部 CodeIgniter 对象的加载方法来实例化数据库对象,但我希望插入或更新产品作为有点混蛋的工作单元模式的一部分。我不需要使用事务,因为 Product 对象不依赖于任何其他 Product 对象的成功插入或更新(使用 Product 对象的类也不依赖于)。但是,我不想为要插入或更新的每个 Product 对象创建一个全新的连接,也不想将其与持久数据库连接联系起来。我宁愿做一些类似于以下的事情:
// catalog class is just an example
class Catalog extends CI_Model
{
public function updateCatalog(array &$products, $dbconn = null) {
if ($dbconn === null) {
try {
$dbconn = $this->load->database('default', true);
} catch (SQLException $e) {
show_error($e->getMessage);
}
$close_db = true;
}
foreach ($products as $product) {
try {
// use the same db connection for all product storage tasks
$product->store($dbconn);
} catch (SQLException $e) {
show_error($e->getMessage);
}
}
if ($close_db) {
$dbconn->close(); // free up database connection for other tasks
}
}
...
这是一个非常基本的示例。如果另一种方法需要调用上述方法,它将实例化数据库连接对象本身,并将其传入。理想情况下,我的目标是每个总体请求有一个数据库连接。
有没有人深入研究 CodeIgniter 源代码来知道我应该使用什么作为数据库对象的类型提示?我正在做的与 CodeIgniter 一起使用的方法是否不好?通过实现连接池的方法来覆盖 CodeIgniter 功能会更好吗?
I'm fairly new to CodeIgniter, and am trying to figure out something I feel should be easy that isn't.
My application does a bit with Product objects, which at the momenet I'm just doing via the ActiveRecord features included within CodeIgniter's Database class. I normally prefer ORM over ActiveRecord, so as to properly separate business logic from database logic. In this case though, the objects that use Products generally handle all the business logic themselves, so I think the separation is still valid.
My Product objects interact with the database through a store()
method which I'd like to have accept a database connection as a parameter. The store()
method first queries the database to determine if the product in question already exists in some form. If so, the product is updated with new information, and if not the product is inserted. Easy stuff. The problem is, I want to use type hinting for the store()
method parameter, so that I can be sure that no objects incapable of basic CodeIgniter database/ActiveRecord functions are passed in to the method without the appropriate error being raised.
For the life of me, I can't figure out the type of the CodeIgniter database class. After reading through the source code a bit, I thought for a while that the CI_DB_driver class was the one. However, when I use this as a type hint, my IDE (NetBeans 7.01, to be exact) doesn't recognize or give me any code completion options for ActiveRecord functions like insert()
or get_where()
. I don't think my IDE is at fault in this, in that it does recognize methods like query()
or insertString()
.
I could use the standard CodeIgniter way of accessing/connecting to databases, i.e. using the internal CodeIgniter object's load method to instantiate a database object, but I was kind of hoping to insert or update Products as part of a somewhat bastardized Unit of Work pattern. I don't need to use transactions, since Product objects do not depend on the successful insertion, or update of any other Product objects (nor do the classes using Product objects). However, I don't want to create a brand new connection for every single Product object to be inserted or updated, nor do I want to tie things up with a persistent database connection. I'd rather just do something more akin to the following:
// catalog class is just an example
class Catalog extends CI_Model
{
public function updateCatalog(array &$products, $dbconn = null) {
if ($dbconn === null) {
try {
$dbconn = $this->load->database('default', true);
} catch (SQLException $e) {
show_error($e->getMessage);
}
$close_db = true;
}
foreach ($products as $product) {
try {
// use the same db connection for all product storage tasks
$product->store($dbconn);
} catch (SQLException $e) {
show_error($e->getMessage);
}
}
if ($close_db) {
$dbconn->close(); // free up database connection for other tasks
}
}
...
This is a very basic example. If another method needed to call the above method, it would instantiate the database connection object itself, and pass it in. I'm aiming for one database connection per overall request, ideally.
Has anyone delved deeply enough into the CodeIgniter source code to know what I should use as a type hint for database objects? Is what I'm doing a bad approach to use with CodeIgniter? Would it be better to override the CodeIgniter functionality with a means of implementing connection pooling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$this->load->database()
返回的数据库对象的类型实际上是 CI_DB_driver,这正是我所希望的。实际上,类型是 CI_DB_(插入您的数据库类型)_driver,但它继承自 CI_DB_driver。我不知道这是否会对任何人有帮助,但我将其放在那里以防万一。
The type of the database object returned by
$this->load->database()
is in fact CI_DB_driver, which is what I was hoping it would be. In actuality the type is CI_DB_(insert your database type)_driver, but this inherits from CI_DB_driver.I don't know if this will ever be helpful to anyone, but I'm putting this out there just in case.
不确定类型提示是什么意思,但看起来您不想使用 CodeIgniter 数据库类。那么为什么不包含您喜欢的外部 ORM 类而不是使用 CodeIgniter 呢?
查看以下资源:
Not sure what you mean by type hint, but it looks like you do not want to use the CodeIgniter Database Class. So why don't you include external ORM class you like instead of messing around with CodeIgniter?
Check out the following resources: