Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Int value converted into string using query builder?
  • Hello this QB expression
    $comments = DB::select('comments.*', 'comment_votes.type', 'users.profile_fields')->from('comments')
     ->join('comment_votes','LEFT')
     ->on('comments.id', '=', 'comment_votes.comment_id')
     ->on('comment_votes.user_id', '=', $this->current_user->id)
     ->join('users','LEFT')
     ->on('comments.user_id', '=', 'users.id')
     ->where('article_id', $article->id);
    

    produces something like
    SELECT `comments`.*, `comment_votes`.`type`, `users`.`profile_fields` FROM `comments` LEFT JOIN 
    `comment_votes` ON (`comments`.`id` = `comment_votes`.`comment_id` 
    AND `comment_votes`.`user_id` = `1`) LEFT JOIN `users` 
    ON (`comments`.`user_id` = `users`.`id`) WHERE `article_id` = '3' 
    

    Why is the
    ->on('comment_votes.user_id', '=', $this->current_user->id)
    giving me out this
    ON (`comments`.`id` = `comment_votes`.`comment_id` AND `comment_votes`.`user_id` = `1`)
    (notice ID being surrounded by`value`)? How to put a value there?
  • Moved to the General section, this has nothing to do with ORM.
  • The Database Query Builder is originally made for MySQL, for which this is no problem. What platform and which driver are you using, and what is the error you are getting?
  • Harro Verton wrote on Tuesday 7th of February 2012:
    The Database Query Builder is originally made for MySQL, for which this is no problem. What platform and which driver are you using, and what is the error you are getting?

    I am using it with MySQL and it is a problem. It treats my value as a table field. The error I get is #1054 - Unknown column '1' in 'on clause' which makes sense since it wrapped the value around `` quotes ... and MySQL wraps table and field names around these quotes...
  • Ah, wait, it isn't surrounded by quotes, it's surrounded by backticks. Yes, the on() method expects two field names, and therefore escapes them. You can avoid that by using DB::expr().
    ->on('comment_votes.user_id', '=', DB::expr($this->current_user->id))
    
  • Ah sorry for the wrong expression... I didn't know how the word for backticks... Thanks for helping yet again!

Howdy, Stranger!

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

In this Discussion