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

StackOverflow https://stackoverflow.com/questions/23083673

  •  04-07-2023
  •  | 
  •  

Question

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: http://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

Was it helpful?

Solution

You can't return the model json_encoded. To do this without modifying your code you need to convert the model to an array before returning it.

try

return $artist->toArray();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top