如何在 FuelPHP 中处理单个 ORM 查询结果?

发布于 2024-11-30 04:58:59 字数 432 浏览 3 评论 0原文

我有一个返回单个结果的查询,我想知道是否有一种方法可以访问该结果的属性而不必循环它,因为我将其限制为单个结果。

这是我的查询:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-12-07 04:58:59

您尝试过

$user->get_one()吗?

Did you try

$user->get_one()?

一腔孤↑勇 2024-12-07 04:58:59

你也可以做祝

$user = Model_User::find_by_email_address_and_password(Input::post('email_address'), Input::post('password'));

你有美好的一天:)

You could also do

$user = Model_User::find_by_email_address_and_password(Input::post('email_address'), Input::post('password'));

Have a nice day :)

禾厶谷欠 2024-12-07 04:58:59

Uku Loskit 询问你正确的语法。
如果你想总是检索单个结果,你可以合并代码:

$user = Model_User::find()
    ->where('email_address', Input::post('email_address'))
    ->where('password', Input::post('password'))
    ->get_one();

给你的建议:小心直接使用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:

$user = Model_User::find()
    ->where('email_address', Input::post('email_address'))
    ->where('password', Input::post('password'))
    ->get_one();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文