关于DataMapper中自定义模型方法的问题
我最近决定在我的 CI 应用程序中实现 DataMapper,到目前为止效果很好。我有几个关于使用自定义模型方法的问题。假设我想在用户登录我的应用程序时更新他的 cookie。我的控制器看起来像这样(我已经排除了类定义):
if(isset($_POST['login_rememberme']) && $_POST['login_rememberme'] == TRUE)
{
// There's already a user object defined here called $user
// Create two new random cookies
$pid = rand_str(40);
$sid = rand_str(40);
// Set the cookies in the users browser
setcookie('pid', $pid, COOKIE_EXPIRE, '/', FALSE, '', TRUE);
setcookie('sid', $sid, COOKIE_EXPIRE, '/', FALSE, '', TRUE);
// Update users relationsship with the new cookies in the db
???
}
cookie 与用户数据(密码、电子邮件等)存储在单独的表中。 cookie 和用户之间的关系在名为 cookies_users 的表中定义。我想我可以在我的“cookie”模型中创建一个名为 setCookies 的方法,并允许两个参数,sid 和 pid,但是我如何使用已经在该方法中建立了用户模型,我是否将其传递给该方法?阅读 DataMapper 后,我不太确定是否建议将所有逻辑放入控制器中并尽可能保持模型干净,或者是否可以使用自定义方法打包模型。有谁知道这个的上下是什么?
感谢您抽出时间。
I recently decided to implement DataMapper in my CI application, it works great so far. I have though a couple of questions regarding the use of custom model methods. Let's say I want to update a users cookies when he logs into my application. My controller looks something like this (I've exlcuded the class definition):
if(isset($_POST['login_rememberme']) && $_POST['login_rememberme'] == TRUE)
{
// There's already a user object defined here called $user
// Create two new random cookies
$pid = rand_str(40);
$sid = rand_str(40);
// Set the cookies in the users browser
setcookie('pid', $pid, COOKIE_EXPIRE, '/', FALSE, '', TRUE);
setcookie('sid', $sid, COOKIE_EXPIRE, '/', FALSE, '', TRUE);
// Update users relationsship with the new cookies in the db
???
}
The cookies are stored in a seperate table from the user-data (password, email etc.). The relationship between the cookies and users are defined in a table called cookies_users. I'm thinking that I could create a method in my 'cookie' model called setCookies, and allow two parameters, sid and pid, but then how do I use the already established user model in that method, do I pass it to the method? Reading the DataMapper I'm not fairly convinced if it's recommended to throw all the logic in the controller and keep the models clean as possible, or if it's okay to pack your models with custom methods. Anybody know what is up and down in this one?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Codeigniter 的优点在于它可以灵活地处理模型或控制器中的内容(从技术上讲,您可以完全放弃模型,但这违背了 MVC 的目的)。关于“胖模型、瘦控制器与胖控制器、瘦模型”的争论有不同的思想流派,但我个人的偏好是在模型中拥有所有直接数据库访问权限,本质上是扩展 DataMapper。不过,我发现,如果您通过 DataMapper 利用可用的函数,通常不需要添加大量自定义函数(一般来说,我最终会得到大约六个函数,其中大多数是只是复杂的选择、验证或调用 API 的 JSON 编码,这些可能会在其他控制器中重用)。
如果您通过对象调用模型函数,您应该能够使用
$this
来访问已建立的对象。从那里,您可以根据需要执行自定义操作。,例如,您的控制器中可能有
$cookie->setCookie($sid, $pid)
,并且在您的模型中可能有(代码未测试,只是一个示例):因此 您的模型中的内容很大程度上取决于您想要完成的任务,以及您想要完成的任务是否可以通过 DataMapper 在本机完成。对于基本的 CRUD 和关系,我通常最终只得到控制器中的对象。
顺便说一句,有许多处理会话的身份验证库。如果您正在尝试构建身份验证系统,您可能需要考虑检查它们。
The nice thing about Codeigniter is that it's flexible about what goes in your models or controllers (technically, you could forego models altogether, but that kind of defeats the purpose of MVC). There are different schools of thought on the "fat model, thin controller vs fat controller, thin model" debate, but my personal preference is to have all direct database access in the models, essentially extending DataMapper. What I've found, though, is that you don't generally need to add a lot of custom functions if you harness the functions available to you through DataMapper (I end up with about half a dozen, generally, and most of them are just complex Selects, validation, or calls to JSON encoding for APIs, which might be reused in other controllers).
If you call the model functions through the object, you should be able to use
$this
to access the established object. From there, you can perform your custom stuff as necessary.So, for example, you might have
$cookie->setCookie($sid, $pid)
in your controller, and in your model you might have (code not tested, just an example):What you have in your models depends largely on what you're trying to accomplish, and whether what you're trying to accomplish can be done natively through DataMapper. For basic CRUD and relationships, I generally end up with just objects in the controller.
As an aside, there are a number of Authentication libraries that handle session. You might want to consider checking them out if you're trying to build an authentication system.