【发布时间】:2011-11-21 20:17:29
【问题描述】:
我正在创建一个服务来获取一些用户数据
class ExampleService{
// ...
public function getValueByUser($user)
{
$result = $this->em->getRepository('SomeBundle:SomeEntity')->getValue($user);
if (!$result instanceof Entity\SomeEntity) {
throw new Exception\InvalidArgumentException("no value found for that user");
}
return $result;
}
}
然后在我的控制器中我有
// ...
$ExampleService = $this->get('example_serivce');
$value = $ExampleService->getValueByUser($user);
我是否应该在此处使用异常来指示在数据库中未找到该用户的值?
如果需要,我该如何处理控制器中 $ExampleService->getValueByUser($user) 返回的内容 - 假设我只想在未找到任何内容(或返回异常)的情况下设置默认值
【问题讨论】:
标签: php exception exception-handling symfony