Forums › Forums › OroPlatform › Exists 'dropManyToOneRelation' in migrations?
This topic contains 8 replies, has 3 voices, and was last updated by spyout98 6 years ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- April 4, 2018 at 10:56 am #37111
There is a method ‘addManyToOneRelation’ for migrations. Exists ‘dropManyToOneRelation’ in migrations?
- CreatorTopic
- AuthorReplies
- April 4, 2018 at 10:58 am #37112
If no, how do I remove a ManyToOneRelation in OroCRM or OroPlatform?
That is my migration code:
PHP1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?phpnamespace Ibv\Bundle\SmcContactBundle\Migrations\Schema\v1_3;use Doctrine\DBAL\Schema\Schema;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;use Oro\Bundle\EntityBundle\EntityConfig\DatagridScope;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\QueryBag;class CreateAssociationWithRelationshipProgram implements Migration, ExtendExtensionAwareInterface{protected $extendExtension;public function setExtendExtension(ExtendExtension $extendExtension){$this->extendExtension = $extendExtension;}public function up(Schema $schema, QueryBag $queries){$table = $schema->createTable('smccontact_relationshipprogram');$table->addColumn('name', 'string');$table->addColumn('label', 'string');$table->setPrimaryKey(['name']);$table->addUniqueIndex(['label'], 'index_label_in_smccontact_relationshipprogram');$this->extendExtension->addManyToOneRelation($schema,'orocrm_contact','relationship_program','smccontact_relationshipprogram','label',['extend' => ['owner' => ExtendScope::OWNER_CUSTOM,'nullable' => true,'on_delete' => null],'view' => ['is_displayable' => true]]);}}I want to destroy this relationship in another migration.
April 4, 2018 at 11:57 am #37113?
April 4, 2018 at 12:22 pm #37114Same question on GitHub
April 4, 2018 at 12:29 pm #37115I’ve created there too. Because I don’t know exactly where is the best place for this kind of question.
April 4, 2018 at 12:31 pm #37116Either one is good place. Linking them for the conveninence of others looking for the same answer later.
April 4, 2018 at 12:41 pm #37117Anyway, how do I do this? To remove completely the relation in orocrm_contact table and others possible auxiliar tables and records in another tables that Oro may created?
April 5, 2018 at 4:22 am #37118Hi @spyout98
The custom relation config can be removed with ‘RemoveOneToManyRelationQuery’, so the relation will not be available in UI
For example, the migration I’ve been using with ‘User’ entity to check:PHP12345678910111213141516171819namespace Oro\Bundle\UserBundle\Migrations\Schema\v2_3;use Doctrine\DBAL\Schema\Schema;use Oro\Bundle\EntityConfigBundle\Migration\RemoveOneToManyRelationQuery;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\QueryBag;use Oro\Bundle\UserBundle\Entity\User;class TestMigrationRemove implements Migration{/*** {@inheritdoc}*/public function up(Schema $schema, QueryBag $queries){$queries->addQuery(new RemoveOneToManyRelationQuery(User::class, 'o2m'));}}But in general ‘RemoveOneToManyRelationQuery’, ‘RemoveManyToManyRelationQuery’, ‘RemoveManyToOneRelationQuery’ removes ONLY configuration of an extend relation, so the data fields are still remains in database.
You can execute the command
app/console doctrine:schema:update --dump-sql
and the result will be something likeMySQL123456ALTER TABLE oro_user DROP FOREIGN KEY fk_oro_user_default_o2m_id;DROP INDEX idx_oro_user_default_o2m_id ON oro_user;ALTER TABLE oro_user DROP default_o2m_id;ALTER TABLE orocrm_contact DROP FOREIGN KEY fk_orocrm_contact_user_o2m_id;DROP INDEX idx_orocrm_contact_user_o2m_id ON orocrm_contact;ALTER TABLE orocrm_contact DROP user_o2m_id;so, the removal of indices, foreign keys and columns is still the developers responsibility and you will need to add such drops into migration as well, e.g.
PHP123$table = $schema->getTable('entity_table');$table->dropIndex('index name');$table->dropColumn('column name');April 5, 2018 at 5:43 pm #37119Thank you so much, @Alexander!
- AuthorReplies
The forum ‘OroPlatform’ is closed to new topics and replies.