j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

how to do arithmetic operator in idiorm ? #348

Closed thisnugroho closed 5 years ago

thisnugroho commented 5 years ago

how to i write this in idiORM i already read the documentation but still dont understand,can u guys please help me,sorry for my bad english

i want to wrote this query in idiORM

SELECT cust_name,opening_amt, payment_amt, outstanding_amt
FROM customer 
WHERE(outstanding_amt - payment_amt) as receiveAmt;

thanks in advance.

kkrell2016 commented 5 years ago

There is so arithmetic operation in it? Just a simple select.

tag commented 5 years ago

Chances are good that the query you've posted is invalid, and even if it isn't, it's possible it's not what you intend.

Perhaps this is what you intend?:

SELECT
  cust_name,opening_amt, payment_amt, outstanding_amt, 
  (outstanding_amt - payment_amt) AS receive_amt
FROM customer
;

MySQL doesn't permit aliasing in the where clause. It's not clear which database provider you're using, but this is not unusual.

Putting the subtraction in the WHERE clause will filter out rows where receiveAmt is zero. (Perhaps this is what you intend). If so, cleaner (more explicit) would be:

SELECT
  cust_name,opening_amt, payment_amt, outstanding_amt, 
  (outstanding_amt - payment_amt) AS receive_amt
FROM customer
WHERE
  (outstanding_amt - payment_amt) = 0
;

In IdiORM, use the where_raw() method.

$payments = ORM::for_table('customer')
            ->where_raw('(outstanding_amt - payment_amt) = 0')
            ->find_many();

Lastly, questions like this are better asked on StackOverflow. Not only are there more eyes there, it's a support venue, whereas GitHub issues is better used for bugs/enhancement requests.

thisnugroho commented 5 years ago

got it !!,thanks everyonee