Deleting table using Doctrine2 and Symfony2

前端 未结 4 1837
悲哀的现实
悲哀的现实 2021-01-05 19:04

How can I delete table using Doctrine2 and Symfony2? I\'ve generated entities and updated schema, now I want to delete this structure.

相关标签:
4条回答
  • 2021-01-05 19:31

    Not sure if I understood your question correctly. You deleted an entity and want to delete also its generated table from database? If so:

    You can't do it, because Doctrine2 only cares about tables it knows - that means the ones that are represented by Entities. Since you deleted some Entity from your application, Doctrine no longer thinks the table belongs to your application. There are situations when there are different tables of different applications in the same database. It wouldn't make sense if Doctrine deleted them just because it doesn't know anything about them. It would be racist... but against tables.

    If you just want to drop tables programmatically, you can use raw query. As far as I know, Doctrine doesn't have any method for dropping tables. Or as an alternative, you can do it by hand.

    0 讨论(0)
  • 2021-01-05 19:34

    Just delete the tables which you are no longer using manually...Doctrine totally ignores unmapped tables.

    0 讨论(0)
  • 2021-01-05 19:34

    In Doctrine 2.6.2 you can DROP table by deleting entity class, after migrations:diff you will get new file with DROP TABLE query, migrations:migrate will execute the command dropping desired table. Class that was about to get deleted had relationships with other entities, so I had to brutally (manually) delete all mentions of old entity class in other entities. Just tested.

    0 讨论(0)
  • 2021-01-05 19:51

    You can do a raw sql.

    For example in a Symfony2 controller:

    $em = $this->getDoctrine()->getManager();
    $sql = 'DROP TABLE hereYourTableName;';
    $connection = $em->getConnection();
    $stmt = $connection->prepare($sql);
    $stmt->execute();
    $stmt->closeCursor();
    
    0 讨论(0)
提交回复
热议问题