Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using DB::query() vs Model_User::find() for searches
  • I'm making a page in which I must display either 'all users' or 'friends' (users in a friends table) depending on the values of an input form.

    I get "all users" via Model_User::get_all();
    But, I need to do an inner join to get the friends... So, I'm using "DB::query()->as_object()->execute()" in order to use the corresponding MySQL query.

    My problem is that it seems that when using the get_all() method, the return value is different than DB::query()... Hence, I cannot use the same html code to display the data..

    I'm wondering if anyone has come across this, and how it was solved?



  • Expanding my question:

    I'm running the following query:

    $query =  
    "SELECT * FROM `tmuser` a
     INNER JOIN 
     ( SELECT DISTINCT f1.`id` FROM
                 (SELECT `tmuser1_id` as `id` FROM `friendship` WHERE `tmuser2_id` = '$user_id') AS f1
                 UNION
                  (SELECT `tmuser2_id` FROM `friendship` WHERE `tmuser1_id` = '$value')) b
    ON a.id = b.id AND a.`$column` $operator '$value'";

    // execute query                
    $result = DB::query($query)->as_object()->execute();



    It returns an object with this property:

    protected '_result' => 
    array (size=1)
    ...
    protected '_total_rows' =>
    int
    which cannot be accessed.
    I merely want to run a foreach loop and echo all the values returned by mysql..
    thanks.
  • philipptempelphilipptempel
    Accepted Answer
    If I recall the return value of DB::execute() correctly, then it returns an object of which the properties cannot - and shall not - be access directly, however, you can loop over what is returned because it implements ArrayAccess and Iterator. So you can simply do

    $friends = \DB::query($query)>as_object()->execute();
    foreach ( $fiends as $friend ) {
    echo $friend->tmuser2_id;
    }

    which should give you the expected result. Have a look at the source code of \Fuel\Core\Database_Result to see what the being returned is.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion