You can package all of your where conditions as associative elements in the second parameter of a get_where()
method call.
The greater-than-or-equal-to and less-than-or-equal-to operators merely need to be appended to key string as demonstrated below.
I do not endorse conditionally returning false
when an object is also possible. PHP has long-supported nullable types and specifically offers null coalescing operations. Because row()
returns an object or null
, I recommend that all model methods which return row()
data should either return null
or throw an exception when something "not good" happens while processing.
public function getContraMarking(
string $from_date2 = '',
string $to_date2 = '',
string $acctno2 = ''
): ?object {
if (!$from_date2 || !$to_date2 || !$acctno2) {
return null;
}
return $this->db
->select([
'account_no',
'txn_date',
'SUM(pd_amt) debit',
'SUM(cr_amt) credit',
'COUNT(value_date) total_count'
])
->group_by('account_no, txn_date, pd_amt')
->order_by('txn_date')
->get_where('ut_sbi_reco_paid_master', [
'account_no' => $acctno2,
'txn_date >=' => $from_date2,
'txn_date <=' => $to_date2
])
->result();
}
}
get_where()
is not suitable if you want to use column names on both sides of the operator. In this case, you'll need to use separate where()
methods with false
as the escape parameter.
->where('col_one >=', 'col_two', false)
->where('col_one <=', 'col_two', false)
->get('ut_sbi_reco_paid_master')
mysql
, but your code is notmysql
... – Anatomistmysql
extension does not have those methods... – AnatomistBETWEEN
operator but have no idea how to use it with the database abstraction they use (which seems to be building the query behind the scenes). – Admire