Copy linkedset with object copier
Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.
- learning:
- Create a method used by Object Copier
- level:
- Intermediate
- domains:
- PHP
- min version:
- 2.3.0
In this tutorial, we will see how we can enrich the extension
User actions configurator, to
copy relationships.
Example: When creating a Ticket from a FunctionalCI, we want to
retrieve contacts linked to the Functional CI and link them
automatically to the Ticket.
- class:Ticket
-
public function CopyContactsFromFunctionalCI($oSource) { // This method is supposed to be called from a FunctionalCI. $sSourceClass = get_class($oSource); if ($sSourceClass != 'FunctionalCI') { throw new Exception("Wrong source class '$sSourceClass' : CopyContactsFromFunctionalCI method should be called by a user action on a FunctionalCI!"); } // This method is called twice: // when creation form is displayed: '????' // and when it is saved : 'apply_new' // Copy the links only in one case, to avoid links duplication $sOperation= utils::ReadPostedParam('operation'); if ($sOperation == 'apply_new') { return; } // Get the list of related objects - in this case, the OQL is not needed but it works $oSetSrc = new CMDBObjectSet(DBObjectSearch::FromOQL(" SELECT Contact AS c JOIN lnkContactToFunctionalCI AS l1 ON l1.contact_id = c.id WHERE l.functionalci_id = :id"), array(), array('id'=>$oSource->GetKey())); // A simpler solution is to use: $oSetSrc = $oSource->Get('contacts_list'); $oSetDst = $this->Get('contacts_list'); while ($oSrc = $oSetSrc->Fetch()) { $oLnkDst = MetaModel::NewObject('lnkContactToTicket'); $oLnkDst->Set('contact_id', $oSrc->GetKey()); $oSetDst->AddItem($oLnkDst); } $this->Set('contacts_list', $oSetDst); }
latest/customization/object-copier-linkedset.txt ยท Last
modified: 2024/09/10 10:25 by 127.0.0.1