Пример кода, который проверяет, существует ли Many-To-Many связь и если она не существует - создает ее:
<?php
namespace Acme\Bundle\FavoriteBundle\Migrations\Schema\v1_0;
use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;
use Oro\Bundle\EntityExtendBundle\Tools\ExtendDbIdentifierNameGenerator;
use Oro\Bundle\MigrationBundle\Migration\Extension\NameGeneratorAwareInterface;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
/**
* Class CreateCustomerUserProductFavoriteRelation.
*
* @package Acme\Bundle\FavoriteBundle\Migrations\Schema\v1_0
*/
class CreateCustomerUserProductFavoriteRelation implements
Migration,
ExtendExtensionAwareInterface,
NameGeneratorAwareInterface
{
/**
* @var ExtendExtension
*/
protected $extendExtension;
/**
* @var ExtendDbIdentifierNameGenerator
*/
protected $nameGenerator;
/**
* {@inheritdoc}
*/
public function setExtendExtension(ExtendExtension $extendExtension): void
{
$this->extendExtension = $extendExtension;
}
/**
* {@inheritdoc}
*/
public function setNameGenerator(DbIdentifierNameGenerator $nameGenerator): void
{
$this->nameGenerator = $nameGenerator;
}
/**
* {@inheritdoc}
*/
public function up(Schema $schema, QueryBag $queries): void
{
$associationName = 'favoriteProducts';
$table = 'oro_customer_user';
$targetTable = 'oro_product';
$joinTableName = $this->nameGenerator->generateManyToManyJoinTableName(
$this->extendExtension->getEntityClassByTableName($table),
$associationName,
$this->extendExtension->getEntityClassByTableName($targetTable)
);
if ($schema->hasTable($joinTableName)) {
return;
}
$this->extendExtension->addManyToManyRelation(
$schema,
$table,
$associationName,
$targetTable,
['id'],
['id'],
['id'],
[
'extend' => [
'is_extend' => true,
'owner' => ExtendScope::OWNER_CUSTOM,
'without_default' => true,
'on_delete' => 'CASCADE',
],
'form' => ['is_enabled' => false],
'view' => ['is_displayable' => false],
'dataaudit' => ['auditable' => false]
]
);
}
}