yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

problem in between condition #8696

Closed M-Fares closed 9 years ago

M-Fares commented 9 years ago

what causes the following yii\db\Query to produce the result query shown bellow, notice the between written twice in mysterious way

use yii\db\Query;

$conditions =  array();
$dataProviderQuery = new Query();

$new_condition =  array( 'ec.FKCertificateLevel'=>$CertificatesLevelID);
$conditions = ArrayHelper::merge($conditions, $new_condition);

$dateB = new DateTime(); 
$oldestDate = $dateB->sub(DateInterval::createFromDateString($ageendID.' years'));

$dateB = new DateTime(); 
$youngestDate = $dateB->sub(DateInterval::createFromDateString($agestartID.' years'));

$new_condition = ['between', 'js.BirthDate', $oldestDate->format('Y-m-d H:i:s'), $youngestDate->format('Y-m-d H:i:s')] ;
$conditions = ArrayHelper::merge($conditions, $new_condition);

$SS=  $dataProviderQuery->select(['js.ID', 'concat(js.FirstName," ",js.FatherName," ",js.LastName) as fullname'])
                      ->from('jobseeker as js')
                      ->leftJoin('jobseekerskills', 'js.ID = jobseekerskills.FKJobSeekerID')
                      ->leftJoin('educationalcertificates as ec', 'js.ID = ec.FKJobSeekerID')
                      ->where($conditions)
                      ->groupBy(['js.ID', 'js.FirstName'])
                       ->having(['=','COUNT(*)',count($skillIDs) ])->indexBy('ID')
                      ->createCommand();
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'between' in 'where clause'
The SQL being executed was: 
SELECT COUNT(*) FROM (SELECT `js`.`ID`, concat(js.FirstName," ",js.FatherName," ",js.LastName) as fullname FROM `jobseeker` `js` LEFT JOIN `jobseekerskills` ON js.ID = jobseekerskills.FKJobSeekerID LEFT JOIN `educationalcertificates` `ec` ON js.ID = ec.FKJobSeekerID WHERE `between` BETWEEN 'js.BirthDate' AND '1900-01-01 00:00:00' GROUP BY `js`.`ID`, `js`.`FirstName`) `c`
cebe commented 9 years ago

your merging is wrong, you can not mix hash format with the other syntax.

better add the condition using andWhere().