[TYPO3-mvc] Best practice for bidirectional sync mm relations

Kevin Ulrich Moschallski km at 3digit.de
Wed Aug 4 15:51:51 CEST 2010


Hi list,

i really need your help with bidirectional, sync mm relations in extbase. Currently i implemented this with MM_opposite_field. This works fine until i want to delete a relation.

Let me give you a small overview about my structure:

Portal
	|
	Items
		|
		Offers

I have a portal which has m items belong to it, also the items belong to m portals.

The TCA looks like this:

for portal:

'items' => array(
			'exclude' => 0,
			'label'   => 'Items',
			'config'  => array(
				'type' => 'inline',
				'foreign_table' => 'tx_specialoffersmvc_domain_model_item',
				'MM' => 'tx_specialoffersmvc_portal_item_mm',
                                
				'maxitems' => 99999
			)
		),

and for item:

'portals' => array(
			'exclude' => 0,
			'label'   => 'Portals',
			'config'  => array(
				'type' => 'inline',
				'foreign_table' => 'tx_specialoffersmvc_domain_model_portal',
				'MM' => 'tx_specialoffersmvc_portal_item_mm',
                                'MM_opposite_field' => 'portals',
				'maxitems' => 99999
			)
		),

With this configuration i can assign items to portals and portals to items in BE (sadly there seems to be no way currently to display a selector in IRRE with this configuration).

In the FE i can add new items to portals and all relations and counters in the db are fine, also the BE is in sync.

My only problem now is, when i delete a item, the relation is delete and the counter in the portal db is decreases by one, for all portals (because i remove it from all portals when the item is deleted). The item itself becomes deleted=1, so far so good. But the portals field in the item db stays the number of portals which where assigned.

You may say this should be no problem because the item is deleted, but i want it to be clean to have the possibility to recycle/undelete it later.

Here is my action which deletes the item:

/**
	 * delete action
	 *
         * @param Tx_SpecialoffersMvc_Domain_Model_Item $item
	 */
	public function deleteAction(Tx_SpecialoffersMvc_Domain_Model_Item $item) {
                // remove from all portals
                foreach($item->getPortals() as $itemPortal) {
                    $itemPortal->removeItem($item);
                }
                $item->removeAllPortals();
                $this->itemRepository->remove($item);
                $this->redirect('show', 'Portal', NULL, Array('portal'=>$portal));
	}

And here is the $item->removeAllPortals() method:

/**
	 * Removes all Portals
	 *
	 * @return void
	 */
	public function removeAllPortals() {
		$this->portals = new Tx_Extbase_Persistence_ObjectStorage();
	}

Does anybody has a clue why the $this->portals = new Tx_Extbase_Persistence_ObjectStorage(); action doesn't set the field portals to 0? Or does anybody ever tried a similar relation with extbase.

I hope i described my problem well enough for you to understand.

Thanks for help in advance.

Regards,

Kevin


More information about the TYPO3-project-typo3v4mvc mailing list