doumeba0486 2015-09-18 04:18
浏览 143
已采纳

Eloquent Left Join,获得意想不到的结果

Overview:

I have two tables called event_meta and options.

Options:

enter image description here

Event Meta:

enter image description here

I have a table called Event Meta where the sub information of specific event are stored. Now what I want to happen is to connect the event_meta and options table using leftjoin in eloquent.

Here's my code for that.

public function getMetaValue($key) {

    $event_meta = $this->hasOne('Core\Event\EventMeta', 'event_id')
        ->leftjoin('options', 'options.id', '=', 'event_meta.meta_value')
        ->where('meta_key', '=', $key)
        ->first();

    // If Option[table] value is empty, it will retrieve for Event Meta's[table] value, else It will return false.
    return (empty($event_meta->value)) ? (empty($event_meta->meta_value)) ? false : $event_meta->meta_value : $event_meta->value;
}

Now the problem is.. If I retrieve the meta_key logo...

enter image description here

It retrieves the value from options table called Top Up that has the id of 6

enter image description here

Notice the first character in meta_key logo's meta_value it has the same value in option's id.

enter image description here

I tried changing the 6 to 7, the value's change and it retrieves the Option's Vending POS.

Any solution for this? This isn't supposed to happen though. Since logo's value is waaay longer than the actual id of options.

--- Edit ---

My attempts fixing it while waiting for help.

1st Attempt:

I tried this solution just for the sake it might be fixed. Still does not work.

public function getMetaValue($key) {

    $event_meta = $this->hasOne('Core\Event\EventMeta', 'event_id')
        ->leftjoin('options', function($query) {
            $query->on('options.id', '=', 'event_meta.meta_value');
        })->where('meta_key', '=', $key)
        ->first();
    return (empty($event_meta->value)) ? (empty($event_meta->meta_value)) ? false : $event_meta->meta_value : $event_meta->value;
}
  • 写回答

2条回答 默认 最新

  • duanjian5059 2015-09-19 02:01
    关注

    I've got an answer to this question! It's not because it's Eloquent but it's really on MYSQL query that has a problem. Well technically my fault for not converting it to same type.

    Thanks to this guy's help who answered my related question here.

    https://stackoverflow.com/a/32647701/1699388

    Cast your numeric options.id field to the same type as event_meta.meta_value in your JOIN.

    So what I did is I converted my options.id AS TEXT to have the same type as event_meta.meta_value in my left join query.

    So I casted it like this

    $leftjoin->on(DB::raw('CAST(options.id AS CHAR(1000))'), '=', 'event_meta.meta_value');
    

    Reference for Cast (SO ANSWER): https://stackoverflow.com/a/12127022/1699388

    So what it looks like is this!

    public function getMetaValue($key) {
    
        $event_meta = $this->hasOne('Core\Event\EventMeta', 'event_id')
            ->leftjoin('options', function($leftjoin) {
                $leftjoin->on(DB::raw('CAST(options.id AS CHAR(1000))'), '=', 'event_meta.meta_value');
            })->where('meta_key', '=', $key)
            ->first();
    
        return (empty($event_meta->value)) ? (empty($event_meta->meta_value)) ? false : $event_meta->meta_value : $event_meta->value;
    }
    

    Second Solution is from my boss in work (Credits to him). What he did is he added another AND on leftjoin to convert event_meta.meta_value to an integer by just multiplying it to 1.

    public function getMetaValue($key) {
    
        $event_meta = $this->hasOne('Core\Event\EventMeta', 'event_id')
            ->leftjoin('options', function($leftjoin) {
                $leftjoin->on('options.id', '=', 'event_meta.meta_value');
                $leftjoin->on(DB::raw('CONCAT("", event_meta.meta_value * 1)'), '=', 'event_meta.meta_value');                
            })->where('meta_key', '=', $key)
            ->first();
    
        return (empty($event_meta->value)) ? (empty($event_meta->meta_value)) ? false : $event_meta->meta_value : $event_meta->value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用