سؤال

I am creating a fairly large query using laravel 4 sql builder. it roughly looks like, get data from table

where(yada yada)and where age between(yada, yada)or between(yada, yada) and where(yad yada).

But what I want is this.

where(yada yada)and where (age between(yada, yada)or between(yada, yada)) and where(yad yada).

some code:

    $query = Subject::grabBasic();// start writing query, which results to grab.
    $agelength  = sizeof($fromAgeValue);
    $count      = 0;
    $ageflag    = 0;
    foreach( array_combine($fromAgeValue, $toAgeValue) as   $from=>$to ){
        $count+=1;
        if($ageflag==0){
            if(($from!=null)&&($to!=null)){
                $query = Subject::addfromAgeToAge($query, $from, $to);
                $ageflag=1;                                                 
            }
        }else if(($from!=null)&&($to!=null)){
            $query = Subject::orfromAgeToAge($query, $from, $to);
        }
    }

... keep adding to $query

Subject code:

static function addfromAgeToAge($query, $fromAge, $toAge){
    return $query->whereBetween('Age', array($fromAge, $toAge));
}

static function orfromAgeToAge($query, $fromAge, $toAge){
    return $query->orWhereBetween('Age', array($fromAge, $toAge));
}

So, is there a way to write the addToAge builder function to take in the arrays of fromage and toage values and create a statement, which when added to the rest of the query, will give the desired results. e-g. the sql query will have parentheses in the correct place.

I would also like to do this without raw queries, I suspect there is a way to do this I just haven't been able to find it.

Thanks,

هل كانت مفيدة؟

المحلول

I buckled and used raw queries :(

Here's what it looks like.

$agecounter = 0;// tells me how many from age to age pairs were filed out coorrectly.
    foreach( array_combine($fromAgeValue, $toAgeValue) as   $from=>$to ){
        if(($from!=null)&&($to!=null)){
            $agecounter+=1;
        }
    }
    $agelength  = sizeof($fromAgeValue);
    $ageflag    = 0;
    foreach( array_combine($fromAgeValue, $toAgeValue) as   $from=>$to ){
        if($ageflag==0){
            if(($from!=null)&&($to!=null)){
                $query = Subject::addfromAgeToAge($query, $from, $to, $agecounter);     // ages put in for loop when multiple are added.
                $ageflag=1;                                                 // add or's
                $agecounter -=1;
            }
        }else if(($from!=null)&&($to!=null)){
            $query = Subject::orfromAgeToAge($query, $from, $to, $agecounter);
            $agecounter -=1;
        }
    }

and in my Model:

static function addfromAgeToAge($query, $from, $to, $count){
    if($count>1){
        return $query->whereRaw('( Age between ? and  ?', array($from, $to));
    }
    return $query-> whereBetween('Age', array($from, $to));
}

static function orfromAgeToAge($query, $from, $to, $count){
    if($count==1){
            return $query->orWhereRaw(' Age between ? and  ?)', array($from, $to));
    }
    return $query->orWhereRaw(' Age between ? and  ?', array($from, $to));//('Age', array($fromAge, $toAge));
}

Hope this helps someone else put ()'s in a query builder.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top