English 中文(简体)
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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签