views:

23

answers:

2

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

+1  A: 

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;
}
Casey
A: 

I haven't tested portability, but in native SQL you can do:

DROP TABLE IF EXISTS ...

You can run native SQL queries with Doctrine as well.

takeshin