Forums › Forums › OroPlatform › OroPlatform – Programming Questions › Create OneToOne relationship with migrations
This topic contains 5 replies, has 4 voices, and was last updated by Geoffroy Cochard 7 years, 8 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- January 26, 2015 at 12:29 pm #33703
Hello!
I wan`t create OneToOne relationship between entities using migration, but in the ExtendExtension class i found only addOneToManyRelation, addManyToOneRelation and addManyToManyRelation methods. How i can create OneToOne relationship? - CreatorTopic
- AuthorReplies
- February 2, 2015 at 4:01 am #33704
me neither! Have you found a solution for this yet?
oro/platform(1.6.2) + oro/doctrine-extensions(1.0.7) + symfony/symfony(v2.3.27)
February 2, 2015 at 4:08 am #33705Unfortunately, solutions, except the use of neToMany relationship, I have not found
February 12, 2015 at 11:03 am #33706Hi! To create OneToOne relationship using migration, you can use Doctrine native methods Doctrine\DBAL\Schema\Table::addColumn() and Doctrine\DBAL\Schema\Table::addForeignKeyConstraint()
February 13, 2015 at 3:11 am #33707hi @hugeval, thanks but that is not what I am looking for(probably I wasn’t clear enough in my question)
What I am trying to do is to create a migration extension for my bundle so that other bundles can use it in their migration classes and add the association dynamically – much like you use the noteExtension.This means that my migration extension class (implements ExtendExtensionAwareInterface) needs to use the injected Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension class to add these relationships.
On ExtendExtension the methods for doing this are: addOneToManyRelation, addManyToOneRelation and addManyToManyRelation, but i am missing addOneToOneRelation.
ref.: https://github.com/orocrm/platform/tree/master/src/Oro/Bundle/EntityExtendBundle#add-relation
oro/platform(1.6.2) + oro/doctrine-extensions(1.0.7) + symfony/symfony(v2.3.27)
July 5, 2016 at 5:41 am #33708Hi,
As said @hugeval, to create a OneToOne relationship through migrations i’m using this to link orocrm_contact to partner_table by example.
But when Entity\Extend is generated, methods to manage OneToMany is generated …
My DB schema is correct (FK Ok, Relation OK)PHP1234567891011121314151617181920212223242526272829303132333435363738394041424344454647/*** Add oneToOne to owner (sourTable)*/$options = ['extend' => ['cascade' => $cascade,'is_extend' => true,'owner' => ExtendScope::OWNER_CUSTOM,'without_default' => true,'relation' => [$relationKey => ['cascade' => $cascade]]]];$selfTableName = $sourceTable instanceof Table ? $sourceTable->getName() : $sourceTable;$selfTable = $sourceTable instanceof Table ? $sourceTable : $schema->getTable($sourceTable);$targetTableName = self::PARTNER_TABLE_NAME;$targetTable = $schema->getTable(self::PARTNER_TABLE_NAME);$targetColumnName = 'id';// -- Add relation SourceTable$selfTable->addColumn('partner_id', Type::INTEGER, ['notnull' => false]);$selfTable->addIndex(['partner_id']);$selfTable->addForeignKeyConstraint($targetTable,['partner_id'],['id'],['onDelete' => 'SET NULL']);// -- Add relation - End// -- Add EntityExtend Options$options[ExtendOptionsManager::TARGET_OPTION] = ['table_name' => $targetTableName,'column' => $targetColumnName,];$options[ExtendOptionsManager::TYPE_OPTION] = RelationType::ONE_TO_ONE;$this->extendOptionsManager->setColumnOptions($selfTableName,'partner',$options);// -- Add EntityExtend Options - EndIn database line config is generated like this :
1OroCRM\Bundle\AccountBundle\Entity\Account - partner - oneToOneAnd Entity\Extend in cache about “partner” Relation :
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748<?phpnamespace Extend\Entity;abstract class EX_OroCRMAccountBundle_Account implements \Oro\Bundle\EntityExtendBundle\Entity\ExtendEntityInterface{protected $partner;public function setPartner($value){if ((!$value instanceof \Traversable && !is_array($value) && !$value instanceof \ArrayAccess) ||!$this->partner instanceof \Doctrine\Common\Collections\Collection) {$this->partner = $value;return $this;}foreach ($this->partner as $item) {$this->removePartner($item);}foreach ($value as $item) {$this->addPartner($item);}return $this;}public function removePartner($value){if ($this->partner && $this->partner->contains($value)) {$this->partner->removeElement($value);}}public function getPartner(){return $this->partner;}public function addPartner($value){if (!$this->partner->contains($value)) {$this->partner->add($value);}}public function __construct(){$this->partner = new \Doctrine\Common\Collections\ArrayCollection();}} - AuthorReplies
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.