php try catch model,关于php:未捕获的异常“ PhalconMvcModelException”,带有消息“模型“ Artist”上不存在“ issent”方法”...

我已经通过Phalcon框架构建了一个微型应用程序。

这是我的代码:

index.php:

use Phalcon\Mvc\Micro,

Phalcon\Http

esponse;

$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

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;

}

}

}

当访问API:http://domain.com/artist/1时,它返回艺术家对象的预期json数据,但浏览器标头的状态为500(内部服务器错误)。

我检查了错误日志,它显示:

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

Stack trace:

#0

[internal function]: Phalcon\Mvc\Model->__call('issent', Array)

#1

[internal function]: Artist->issent()

#2

/var/www/localhost/index.php(52):

Phalcon\Mvc\Micro->handle()

#3 {main}

thrown in

/var/www/localhost/index.php on line 52

您无法返回json_encoded模型。 为此,无需修改代码,您需要在返回模型之前将模型转换为数组。

尝试

return $artist->toArray();

这不能为问题提供答案。 要批评或要求作者澄清,请在其帖子下方发表评论。

它确实是在问题的背景下。 它需要更改一行。 我可以复制粘贴他的整个代码块,然后在其中隐藏一行更改。 我也可以附上一篇论文。 我选择不因为缺少一个简单的方法调用而吓beat他。 这可以吗?

谢谢david.duncan。 我尝试了您的建议,它奏效了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值