问题描述
概述:
我有两个表名为 event_meta
和选项
。
选项:
事件元数据
我有一个名为Event Meta的表,其中存储了特定事件的子信息。现在我想要发生的是使用 leftjoin
event_meta
和选项
code>在雄辩中。 这是我的代码。
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)
- > ();
//如果Option [table]的值为空,它将检索Event Meta的[table]值,否则返回false。
return(empty($ event_meta-> value))? (空($ event_meta-> meta_value))? false:$ event_meta-> meta_value:$ event_meta-> value;
}
现在的问题是..如果我检索到 meta_key
logo ...
它从名为的选项表中检索值为$ code> 6
注意 meta_key中的第一个字符
logo的 meta_value
它在选项的id中具有相同的值。
所以我做的是将我的 options.id
AS TEXT转换为与相同的类型event_meta.meta_value
$ b
所以我像这样投下了
$ leftjoin-> on(DB :: raw('CAST(options.id AS CHAR(1000))'),'=','event_meta.meta_value');
Cast(SO ANSWER)参考:
所以看起来像这样!
public function getMetaValue($ key){
$ event_meta = $ this-> hasOne('Core\\ ($ item)'
- > leftjoin('options',function($ leftjoin){
$ leftjoin-> on(DB :: raw('CAST $ id =','=',$ key)
- >其中('meta_key','=',$ key)
;第一();
return(empty($ event_meta-> value))? (空($ event_meta-> meta_value))? false:$ event_meta-> meta_value:$ event_meta-> value;
}
第二个解决方案来自我老板的工作(对他的信用)。他做了什么是在leftjoin上添加了另一个 AND
,将 event_meta.meta_value
转换为一个整数,只需将其乘以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))? (空($ event_meta-> meta_value))? false:$ event_meta-> meta_value:$ event_meta-> value;
}
Overview:
I have two tables called event_meta
and options
.
Options:
Event Meta:
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...
It retrieves the value from options table called Top Up
that has the id of 6
Notice the first character in meta_key
logo's meta_value
it has the same value in option's id.
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;
}
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
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;
}
这篇关于雄辩左派加盟,取得意想不到的成果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!