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

Uncaught exception 'Phalcon\\Mvc\\Model\\Exception' with message 'The method “issent” doesn't exist on model “Artist”'

I have built a micro application by Phalcon framework.

This is my code:

index.php:

    <?php
    use Phalcon\Mvc\Micro,
        Phalcon\Http\Response;

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(array(
        __DIR__ . '/models/'
    ))->register();

    $di = new \Phalcon\DI\FactoryDefault();

    $di->set('db', function(){
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host" => "localhost",
            "username" => "root",
            "password" => "xxx",
            "dbname" => "xxx"
        ));
    });

    $app = new \Phalcon\Mvc\Micro($di);

    $app->after(function() use ($app) {
        echo json_encode($app->getReturnedValue());
    });

    $app->notFound(function () use ($app) {
        $app->response->setStatusCode(404, "Not Found")->sendHeaders();
        echo 'This is crazy, but this page was not found!';
    });

    $app->get('/artist/{id}', function ($id) {
        $response = new Response();

        $response->setHeader("Content-Type", "application/json")->send();
        $artist = Artist::findFirstById($id);

        return $artist;
    });

    $app->handle();

artist.php

    <?php

    use Phalcon\Mvc\Model\Validator\Email as Email;

    class Artist extends \Phalcon\Mvc\Model
    {

        /**
         *
         * @var integer
         */
        public $id;

        /**
         *
         * @var string
         */
        public $title;

        /**
         *
         * @var string
         */
        public $slug;

        /**
         *
         * @var string
         */
        public $content;

        /**
         *
         * @var string
         */
        public $image;

        /**
         *
         * @var string
         */
        public $thumb;

        /**
         *
         * @var integer
         */
        public $views;

        /**
         *
         * @var integer
         */
        public $likes;

        /**
         *
         * @var integer
         */
        public $total_views;

        /**
         *
         * @var integer
         */
        public $total_downloads;

        /**
         *
         * @var string
         */
        public $rank;

        /**
         *
         * @var string
         */
        public $real_name;

        /**
         *
         * @var string
         */
        public $email;

        /**
         *
         * @var string
         */
        public $phone;

        /**
         *
         * @var string
         */
        public $dob;

        /**
         *
         * @var string
         */
        public $death;

        /**
         *
         * @var string
         */
        public $born_address;

        /**
         *
         * @var string
         */
        public $current_address;

        /**
         *
         * @var string
         */
        public $work_address;

        /**
         *
         * @var string
         */
        public $is_group;

        /**
         *
         * @var string
         */
        public $status;

        /**
         *
         * @var string
         */
        public $created_at;

        /**
         *
         * @var string
         */
        public $modified_at;

        /**
         * Validations and business logic
         */
        public function validation()
        {

            $this->validate(
                new Email(
                    array(
                        "field"    => "email",
                        "required" => true,
                    )
                )
            );
            if ($this->validationHasFailed() == true) {
                return false;
            }
        }

    }

When visiting the API: https://domain.com/artist/1, it returns the expecting json data of an artist object, but the status of browser's header is 500 (Internal Server Error).

I checked the error log, it shows:

> PHP Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'The method "issent" doesn't exist on model "Artist"' in /var/www/localhost/index.php:52\nStack trace:\n#0 [internal function]: Phalcon\Mvc\Model->__call('issent', Array)\n#1 [internal function]: Artist->issent()\n#2 /var/www/localhost/index.php(52): Phalcon\Mvc\Micro->handle()\n#3 {main}\n thrown in /var/www/localhost/index.php on line 52


58.4k

Hi

I think error above (Internal Server Error) due to .htaccess. Beside you can refer to source Sticker store is a very simple micro-application making use of the micro-mvc approach Github.

The problem is that you're returning $artist instead of the $response object.

If you check the docs for the Response class, you'll see it has an isSent() method. Phalcon is trying to call this function on your $artist. At the very bottom of the doc page you can also see that the Response class has a function to handle JSON ( setJsonContent )

https://docs.phalcon.io/en/3.0.1/api/Phalcon_Http_Response.html



43.9k

Hi,

you set your headers to tell that you send a json content, but you response is an object.

Use:


        $response = new Response();

        $response->setHeader("Content-Type", "application/json");
        $artist = Artist::findFirstById($id);
        $response->setJsonContent(
           [
               "artist" => $artist,
           ]
        );
        $response->send();