Catalyst 将方法添加到 DBI 模型
如果我有基于 'Catalyst::Model::DBI' 的模型,并且我想要一个具有类似 $c->model('DBI')->my_method( );但 $c->model('DBI') 不会返回对我的该对象的引用,而是返回一个 DBI::db。我可以取回 dbh 并对其进行操作,但我有一堆实用方法,我更愿意在此处添加。
How would I add methods to my DBI model if I have 'Catalyst::Model::DBI' based model and I'd like a method to have something like $c->model('DBI')->my_method(); but $c->model('DBI') doesn't return a ref to my that object, rather I get back a DBI::db. I can get back the dbh and operate on that, but I have a bunch of utility methods that I'd prefer to add here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有看到你的代码,所以我不能确定你在做什么,但是如果你使用 Catalyst::Model::DBI 你做错了什么。原始模型确实返回对象,例如:
MyApp::Model::DBI=HASH(0xdf7ba0)
听起来您可能正在尝试使用适配器内容加载 DBI。子类化 DBI 比您想象的要困难,所以我肯定会回避这一点。
最小再现 -
现在你可以尝试向你的模型添加一些东西
- 并将其添加到你的控制器 -
然后访问
localhost:3000/dbi/add
。继续按照您喜欢的方式扩展您的模型。现在,问题得到了解答。您真的、真的、真的应该立即学习并熟悉 DBIx::Class 或 Perl 中其他一流的 ORM 之一。简单的 DBI 很好,但随着时间的推移,您会发现 DBIC 已经解决了 100 个问题,并且它配备了深入的测试套件、悠久的历史、数十个扩展和一个有用的社区。
I haven’t seen you code so I can’t know for sure what you’re doing but if you’re using Catalyst::Model::DBI you’re doing something wrong. The raw model does return the object, e.g.:
MyApp::Model::DBI=HASH(0xdf7ba0)
It sounds like you might be trying to load DBI with the Adaptor stuff. Subclassing DBI is harder than you might think so I’d definitely shy away from that.
Minimal reproduction–
Now you could try adding something to your model–
And this to your controller–
Then visit
localhost:3000/dbi/add
. Continue to extend your model however you like.Now, that the question is answered. You really, really, really should take the learning hit right now and get familiar with DBIx::Class or one of the other first class ORMs in Perl. Bare bones DBI is fine but you’re going to find 100 problems over time that DBIC has solved and it comes with a deep test suite, a long history, dozens of extensions, and a helpful community.
我自己没有使用直接 DBI 模型,所以我不确定这是否适合您。我使用 DBIC::Schema 模型
这会在模型目录中创建一个数据库模型,它只是保存在 lib/MyApp/Schema.pm 和 lib/MyApp/Schema 中的底层 DBIx::Class::Schema 架构的包装器/Result/
如果我将 foo() 子例程添加到 lib/MyApp/Model/DB.pm 我可以简单地引用它,就像
我认为 DBI 模型也创建了一个包装器模型 $c->model('DBI')- >dbh 应返回原始 DBI 句柄,$c->model('DBI') 催化剂模型包装器
I am not using a direct DBI model myself, so I am not sure this works for you. I use the DBIC::Schema model
This creates a DB model in the Model dir that is merely a wrapper to the underlying DBIx::Class::Schema schema that is saved in lib/MyApp/Schema.pm and lib/MyApp/Schema/Result/
If I add a foo() subroutine to lib/MyApp/Model/DB.pm I can simply reference it like
I thought the DBI model also created a wrapper model, $c->model('DBI')->dbh should return the raw DBI handle, $c->model('DBI') the catalyst model wrapper
下面的代码是我如何构建模型的示例,我已经在以下位置编写了相关教程:
http://brainbuz.org/techinfo。我使用 DBIx::Simple 是为了方便,您可以轻松地跳过它以获取原始 dbi,并直接在模型中引用 $self->dbh 。
The code below is an example of how I build my Models, I've written a tutorial on this at:
http://brainbuz.org/techinfo. I use DBIx::Simple as a convenience, you can easily skip it for raw dbi, and directly reference $self->dbh in your Model.