php - Yii2 MongoDB query not working -
i using mysql db before , moved using mongodb recently. have been converting queries mongodb format encountered problem along way.
i wanted payslip_id
of specific user date_generated
(of payslip) in between start , end dates of specific payroll period. here's original query (mysql):
public function getpayslipdetails() { $time = date_default_timezone_set('asia/manila'); $time = date('y/m/d', time()); $session = yii::$app->session; return $this->hasone(payslip::classname(), ['user_id' => 'user_id'])->where('date_generated between :start_time , :end_time', array(':start_time' => $session['start'], ':end_time' => $session['end'])); } public function getpayslipid() { return $this->payslipdetails ? $this->payslipdetails->payslip_id : ''; }
now, here's converted query mongodb:
public function getpayslipdetails() { $time = date_default_timezone_set('asia/manila'); $time = date('y/m/d', time()); $session = yii::$app->session; return $this->hasone(payslip::classname(), ['user_id' => '_id'])->where(array('date_generated' => array('$in' => array($session['start'], $session['end'])))); } public function getpayslipid() { return $this->payslipdetails ? $this->payslipdetails->_id : ''; }
notice return statements different. wonder if mongodb query correct or not. doesn't return error well.
i appreciate response.
the $in
operator selects documents value of field equals value in specified array. not equivalent mysqlbetween
.
public function getpayslipdetails() { $time = date_default_timezone_set('asia/manila'); $time = date('y/m/d', time()); $session = yii::$app->session; return $this->hasone(payslip::classname(), ['user_id' => '_id'])->where(array('date_generated' => array('$gte' => $session['start']), 'date_generated' => array('$lte' => $session['end']))); } public function getpayslipid() { return $this->payslipdetails ? $this->payslipdetails->_id : ''; }
i not sure syntax of php-mongodb think should help.
Comments
Post a Comment