catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Can't use Medoo::raw("WHERE ...") in db->count($table, $where) $where arg #726

Closed pedrofsantoscom closed 6 years ago

pedrofsantoscom commented 6 years ago

Hi there,

I'm trying to do a complex sql where clause, so i'm using the Medoo::raw(). The issue is in with the $where argument for the $db->count() function, it just doesn't build the where clause:

$result = $db->debug()->count(
                $this->table,
                Medoo::raw(
                    "WHERE
                    <user> = :user
                    AND
                    (
                        (
                            YEARWEEK(<date>) = YEARWEEK( CURDATE() )
                            AND DAYOFWEEK(<date>) = 1
                        )
                        OR
                        (
                            YEARWEEK(<date>) = YEARWEEK( CURDATE() ) + 1
                            AND DAYOFWEEK(<date>) = 1
                        )
                    )", [":user" => $user]));

Count debug output:

SELECT COUNT() FROM `table_name`

But using $db->select() it does indeed build the raw sql:

$result = $db->debug()->select(
                $this->table,
                ["count" => Medoo::raw("COUNT()")],
                Medoo::raw(
                    "WHERE
                    <user> = :user
                    AND
                    (
                        (
                            YEARWEEK(<date>) = YEARWEEK( CURDATE() )
                            AND DAYOFWEEK(<date>) = 1
                        )
                        OR
                        (
                            YEARWEEK(<date>) = YEARWEEK( CURDATE() ) + 1
                            AND DAYOFWEEK(<date>) = 1
                        )
                    )", [":user" => $user]));

Select debug output:

SELECT COUNT(*) AS `count` FROM `table_name` WHERE `user` = 'user1' AND ( ( YEARWEEK(`date`) = YEARWEEK( CURDATE() ) AND DAYOFWEEK(`date`) = 1 ) OR ( YEARWEEK(`date`) = YEARWEEK( CURDATE() ) + 1 AND DAYOFWEEK(`date`) = 1 ) )

Is this the supposed behaviour? Would be awesome if the Medoo aggregate functions would build the raw sql

catfan commented 6 years ago

Yes, that's right. I made a fix for that. 87ab8ba

The raw object can be used it on aggregate function now.

pedrofsantoscom commented 6 years ago

Thank you catfan! 👍