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

Some way to reset the select array #119

Closed andrewfenn closed 11 years ago

andrewfenn commented 11 years ago

Perhaps I am misunderstanding how the libraries are suppose to work so I'll quickly run through my issue.

I am currently testing out this library on some tables which have has_many_through relationships via paris. That's all working nicely however when I call count() on the query and I inspect the resulting SQL I get something like this..

SELECT `users`.*, COUNT(`users`.`uid`) AS `count` FROM `users` ......

When I was expecting just the count..

SELECT COUNT(`users`.`uid`) AS `count` FROM `users` ......

Is there some way to reset the select array to make this work? Right now I've basically hacked idiorm by adding a function to reset the select array.

treffynnon commented 11 years ago

Currently there is not a way to reset the select array. You're certainly not the first person to highlight this issue. I am open to pull requests that resolve it.

The way to do this at the moment is to use PHP's clone language construct. So you build your query (without any calls to select()) and then clone it to a new variable. You then have two instances of the same query - on one you can now add any select() calls and finally call find_many() et. al.

On the other instance you can call count().

In this way you have effectively forked the query at the point where they diverge. It is not ideal, but it works and it is the method I use at the moment in the rare occurences I encounter this shortcoming.

Hope it helps and like say if you see a solution I would welcome a pull request.

:smile:

andrewfenn commented 11 years ago

I'm currently still just evaluating the library for use in a project my hack was to simply add

        public function select_reset()
        {
            $this->_result_columns = array();
        }

to ORM which I then call before calling count. If I end up using the library I'll send some pull requests, but right now I am super busy. :)

treffynnon commented 11 years ago

Yeah I am hoping to add a way to fork a query at any point rather than just killing the select array. Presently however I am caught up in client work and don't have enough time to write it. Anyway let me know anything else you find.