Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 372
Casey Avatar asked Jul 10 '10 22:07

Casey


2 Answers

Doctrine2 method is:

$schemaManager = $this->getDoctrine()->getConnection()->getSchemaManager();
if ($schemaManager->tablesExist(array('users')) == true) {
      // table exists! ...
}
like image 22
pleerock Avatar answered Oct 14 '22 10:10

pleerock


Try this for table exist check:

 public function up(Schema $schema): void
{
    $schema->hasTable('table_name');
}

For columns check:

$schema->getTable('supply')->hasColumn('contracts')

Full example with skip:

$this->skipIf($schema->getTable('supply')->hasColumn('contracts'), 'Table order_statuses already exist');
like image 51
Georgy Avatar answered Oct 14 '22 11:10

Georgy