Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Display results of Builder

Hi guys, I'm learning to use the Builder and so far I've been using it to paginate stuff and it works great. However, now I don't need to paginate, I need to put every row returned into an array (later to export it to an Excel file) and I haven't been able to manipulate the Builder results in order to do this.

Here's my code:

    $columnsSt = 'Customers.partner_id,  Customers.customer_id, account_type, merge, DATE_FORMAT(MAX(date), "%d-%m-%Y") as lastdate';



  $customers = $this->modelsManager->createBuilder()

                ->columns($columnsSt)

                ->from('Customers')

                ->leftJoin('Purchases', 'Customers.customer_id = p.customer_id','p')

                ->groupBy(array('Customers.customer_id'))

                ->orderBy('customer_id DESC');

As I mentioned, if I use the paginator, I can display this info on my page without problem:

$paginator = new PaginatorQueryBuilder(array(

                "builder"   => $customers,

                "limit"     => 20,

                "page"      => $this->request->getQuery('page', 'int')

            ));

However, now I don't want to paginate! I want to put every result of the Builder into an array. Something like this:

foreach($customers as $cust)
    {

        $CustArr[] = array(
            'partner_id' =>  $cust->partner_id,
            'account_type'          =>  $cust->account_type,
            'lastdate'    =>  $cust->lastdate,

        );

    }

So my question is: how can I manipulate the results of the Builder in order to place them in an array? I'm sorry if this is too dumb, but I haven't been able to do this. Thank you so much!

try adding this after your orderBy clause

// Get all results
->getQuery()->execute();

// Get single result
->getQuery()->getSingleResult();

// Pro tip: caching the query
->getQuery()->cache(['key' => 'your-cache-filename'])->execute();


1.4k
Accepted
answer

Thank you so so much!! Just a little question more, what's the advantage or... what can I do exactly when caching the query? Thanks again!

try adding this after your orderBy clause

```php // Get all results ->getQuery()->execute();

// Get single result ->getQuery()->getSingleResult();

// Pro tip: caching the query ->getQuery()->cache(['key' => 'your-cache-filename'])->execute(); ```

Ill quote Phalcon docs:

Accessing database systems is often one of the most common bottlenecks in terms of performance.

Its good idea to cache common queries which dont change often. For example a good idea for cache would be sections on your homepage like latest articles, carousels, menu e.t.c.

More info on caching in the docs: https://docs.phalconphp.com/en/latest/reference/models-cache.html

If you don't have more questions you can mark thread as solved.