Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
trying to get property of non object issue
  • I am running this function:

    public static function get_friendsingroup($group_id) {
            if (! $user_id = Auth::get('id')) {
                return 0;
            }
            $query =
                "SELECT * FROM "
                . "(SELECT a1.* FROM `tmuser` a1 INNER JOIN (SELECT `tmuser_id` FROM `tmgroups_tmusers` WHERE `tmgroup_id` = '$group_id') b1 ON a1.`id` = b1.`tmuser_id`) members, "
                . "(SELECT a2.* FROM `tmuser` a2 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` = '$user_id')) b2 ON a2.id = b2.id) friends "
                . "WHERE members.id = friends.id";
            
            return DB::query($query)->execute();
            
        }


    Then in the view, I'm using a foreach loop to display the properties:

    <?php foreach ($tmuser as $item): ?>
                <tr> <td><?php echo $item->username; ?></td>    // fails here
    <td><?php echo $item->email; ?></td>    
                             ...etc

    Then I get an error: 
    "Trying to get property of non object"




    I do a var_dump() on the result of the query and it returns an object with a protected _result instance variable, and inside all the results... I ran the query on mysql, and it works... but Idk why fuelphp doesn't like it

    What am I doing wrong?
  • Nevermind... I set the query to return ->as_object() and it solved the issue..
  • ginsamaginsama
    Accepted Answer
    try to use query builder that's more simple and also I think you are using REST controller then a return $this->response() is what you need to return a json/xml results. check here http://fuelphp.com/docs/general/controllers/rest.html

Howdy, Stranger!

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

In this Discussion