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

How to manipulte results of Query Builder 'createBuilder()'

Hi guys! So I have something like this:

$columnas = 'id, email, DATE_FORMAT(register_date, "%d-%m-%Y %H:%i:%s") as register_date';

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

                ->columns($columnas)

                ->from('Table1')

                ->orderBy('register_date DESC');

Now I want to manipulate what I get in $results. I want to create a separate ARRAY with just the emails from this resulting query.

I was thinking on something like this:

foreach($results as $result){

$email=$result->email;

array_push($arrayEmails, $email);

}

But this isn't working. I want to know how to access the elements from my $results . Can you pelase point me in the right direction? Thank you!!

edited Apr '16

First you need to getQuery and execute it:

    $results = $this->modelsManager->createBuilder()
                ->columns($columnas)
                ->from('Table1')
                ->orderBy('register_date DESC')
                ->getQuery()
                ->execute();

Then you can do what you want.

Also you sure you have model Table1 ? Pretty weird name for model name. Also you are not using any namespaces ?



4.3k

Thank you four your response! Yeah, Table1 is no the name of the actual table, I just put that there as an example, sorry for the confusion. Doing your suggestion didn't help at all, now for some reason $results is NULL, something that wasn't happening before :(

First you need to getQuery and execute it:

   $results = $this->modelsManager->createBuilder()
               ->columns($columnas)
               ->from('Table1')
               ->orderBy('register_date DESC')
              ->getQuery()
              ->execute();

Then you can do what you want.

Also you sure you have model Table1 ? Pretty weird name for model name. Also you are not using any namespaces ?



145.0k
Accepted
answer

It's null beacause no record are returned :)

Did you even have query executed ? Check your logs. This code i posted in necessary in modelsManager:

                ->getQuery()
                ->execute();

It's just executing query. Without this you wasn't even executing your query.



4.3k
edited Apr '16

Mmm This is weird :( So this is the code I'm using in the Controller, it works perfectly:

        $customers = $this->modelsManager->createBuilder()
                ->columns($columnas)
                ->from('Customers')
                ->orderBy('register_date DESC');

            $paginator = new PaginatorQueryBuilder(array(
                "builder"   => $customers,
                "limit"     => 20,
                "page"      => $this->request->getQuery('page', 'int')
            ));

So in my view I display the data from that query with a foreach... but using the paginator, in this case 'page.items'. (Example: I'd use obj.page.items.email to display the email data)

However, I want to do some more stuff on the Controller, I want to manipulate some data from $customers, as I mentioned I can't use foreach with $customers. With this code I just posted, $customers returns data, no problem, but when I added the code you mentioned it returns nothing. Sorry about all the confusion, I really appreciate it, if you have any advice on this, pleasee help :)

Next time post full code. Paginator is already doing what i wrote so there no reason to add what i wrote. Im just not using paginator.

Whats your problem exactly ? What error you have in foreach ?