如何在 FuelPHP 中处理单个 ORM 查询结果?
我有一个返回单个结果的查询,我想知道是否有一种方法可以访问该结果的属性而不必循环它,因为我将其限制为单个结果。
这是我的查询:
$user = Model_User::find()
->where('email_address', Input::post('email_address'))
->where('password', Input::post('password'))
->limit(1);
我发现访问结果的唯一方法是在 $user
上运行 get()
方法并循环结果,但我认为我错过了一些东西,并且有一种更简单的方法可以将 $user
作为我可以使用的单个对象返回,因为我将其限制为单个结果。
最有效的方法是什么?
I have a query that is returning a single result and I'm wondering if there's a way to access the properties of that result without having to loop through it since I am limiting it to a single result.
Here is my query:
$user = Model_User::find()
->where('email_address', Input::post('email_address'))
->where('password', Input::post('password'))
->limit(1);
The only way I've found to access the results is to run the get()
method on $user
and loop through the result, but I figured I was missing something and that there was an easier way to return $user
as a single object that I can work with since I am limiting it to a single result.
What's the most efficient way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试过
$user->get_one()
吗?Did you try
$user->get_one()
?你也可以做祝
你有美好的一天:)
You could also do
Have a nice day :)
Uku Loskit 询问你正确的语法。
如果你想总是检索单个结果,你可以合并代码:
给你的建议:小心直接使用Input::post('var_name'),最好在保存变量之前使用验证。
另一种方法是将框架设置为对每个 $_GET 和 $_POST 变量执行某些操作,例如 htmlentities()。
Uku Loskit ask you the right syntax.
If you want to retrieve always a single result, you can merge the code:
An advice for you: be careful using directly Input::post('var_name'), it would be better to use a validation before saving variables.
Another way is to set the framework to perfrom some action, like htmlentities(), for every $_GET and $_POST variable.