We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using many values with a Phalcon query

Hi guys, I'm getting used to the Model stuff with phalcon. I want to do a query similar to this one:

SELECT pid, MAX(date) as last

        FROM Purchases

        WHERE pid IN ('$strIDQuery')

        GROUP BY pid

Here $strIDQuery=('09282',11811','32762')

So as you can see, I'm trying to retrieve the last date from each ID I pass trough the array. How can I do this with Phalcon:

I have this code:

$lastDates = Purchases::findFirst(

            array(

                "conditions" => "pid = '09282'",

                "order" => "date DESC",

                'format' => 'd-m-Y'

            )

        );

This code returns the last date of the pid=09282, BUT I want to pass many values and get a list of the last dates of each value. (What I'm doing with the IN command with my query above).

So, how can I achieve the results of the query above, with the phalcon models? Thank you!

edited May '16

Try something like this

$lastDates = Purchases::find(array(
    "columns" => "pid, MAX(date)"
    "conditions" => "pid IN :ids:",
    "bind" => array("ids" => $strIDQuery),
    "group" => "pid"
));

if this fails use query

$lastDates = Purchases::query()
    ->columns(array("pid", "MAX(date)"))
    ->inWhere("pid", strIDQuery)
    ->groupBy ("pid")
    ->execute();