How do you work with a single ORM query result in FuelPHP?
题
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?
解决方案
Did you try
$user->get_one()
?
其他提示
You could also do
$user = Model_User::find_by_email_address_and_password(Input::post('email_address'), Input::post('password'));
Have a nice day :)
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.