开发者

Best way to catch Doctrine Exceptions in Codeigniter

What do you think is the best way to catch all doctrine 1.2 ORM exceptions in codeigniter framework, i would not like to wrap the entire index.php with a try catc开发者_Go百科h, but neither to do a try catch before and after every query,


Well, first, you'll have to wrap around only single line in index.php. And actually, this might be good in case you have exceptions you don't want to show (e.g. in production environment).

The second point here is that your database-related code should be concentrated in models. So you might introduce helper class, which is something like

class SafeQueryHelper{
    public static function safeQueryRun(Doctrine_Query $q, array $parameters, $hydration=Doctrine_Core::HYDRATE_RECORD){
        try{
            return $q->execute($parameters, $hydration);
        }
        catch(Exception $e){
            //Handle yur exceptions here
        }
    }
}

Than you'll just replace $query->execute($params,$hydration) in all your models to SafeQueryHelper::safeQueryRun($query,$params,$hydration). Don't forget to load it with $this->load->helper('SafeQueryHelper') or through the config.

For record methods like update and delete - you'll have to wrap it in try .. catch.

Well, and if you don't have your database-related logic concentrated in models... That changes nothing, actually, but that means that you have poorly-designed application that violates the essential priinciple of MVC pattern, so start refactoring.

The last possible solution - is to hack into Doctrine core classes (specifically into Doctrine_Connection) and wrap into try ... catch lines that perfrom actual quering. But that's a bad idea, I really wouldn't do that.

A little update: as all Doctrine entity objects are subclaeses of Doctine_Record youmay extend SafeQueryHelper with methods for wrapping save, delete etc:

public static function SafeSave(Doctrine_Record $entity){
    try{
        $entity->save();
    }
    catch(Exception $e){
        //catch it
    }
}

Than replace $entity->save() with SafeQueryHelper::SafeSave($entity)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜