YK. toe
YK. toe

Reputation: 15

How to execute query in phalcon model

iam using phalcon, I tried to execute query from controller, query run my model

    <?php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Query;

class CakupanBu extends Phalcon\Mvc\Model
{
    public static function getJenisBU()
    {
        header("Access-Control-Allow-Origin: *");
        header('Content-type:application/json;charset=utf-8');
    $data = array();
    $query = new Query(
        'SELECT id_jenis_bu,count(jumlah_bu) as jumlah FROM CakupanBu group by id_jenis_bu',
        $this->getDI()
    );
    // Execute the query returning a result if any
    $jbus = $query->execute();

    foreach ($jbus as $jbu) {
        $data[] = array(
            'id_jenis' => $jbu->id_jenis_bu,
            'jumlah' => $jbu->jumlah,
        );
    }

        return json_encode($data);

    }

}

But sadly this is not working, and I ended up with an error.

<b>Fatal error</b>:  Uncaught Error: Using $this when not in object context in 'CakupanBU.php:14'

from controller i call :

 $jbus=CakupanBU::getJenisBU();

Could anyone give me solution? thanks you

Upvotes: 0

Views: 558

Answers (1)

Ultimater
Ultimater

Reputation: 4738

Your function is static, so there is no $this.
To get the DI, you would replace your $this->getDI() with \Phalcon\DI::getDefault()

Upvotes: 2

Related Questions