doctrinedoctrine-1.2

Check for table existence before dropping?


I'm trying to check for the existence of a table before dropping it. I've read through the API documentation for Doctrine_Table and I can't seem to find anything like this. Is there something I'm missing?

I've got code that looks like:

$table = new Doctrine_Table('model_name', $conn);

$export = new Doctrine_Export();

$export->dropTable($table->getTableName());

And the error I get when a table doesn't exist is:

Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table

Thanks in advance,

Casey


Solution

  • Here's what I wound up using... any suggestions for improvement are welcome:

    public static function isInstalled()
    {
        $installed = true;
    
        $q = Doctrine_Query::create($conn);
        $q->select('t.id');
        $q->from('Table t'); //the table to check
    
        try {
            $q->execute();
        } catch (Doctrine_Connection_Exception $e) {
            // we only want to silence 'no such table' errors
            if ($e->getPortableCode() !== Doctrine_Core::ERR_NOSUCHTABLE) {
                throw new Doctrine_Export_Exception($e->getMessage());
            }
    
            $installed = false;
        }
    
        return $installed;
    }