0
votes

I want to "empty" an ObjectStorage when updating a Object:

It's TYPO3 4.6 with a Extbase Extension which allows you to show/add/edit/delete datasets in the frontend. At first sight everything looks good.

I have one field referencing another table:

TCA:

'partner' => array(
'exclude' => 0,
'label' => 'LLL:EXT:toco3_marketingdb/Resources/Private/Language/locallang_db.xlf:tx_toco3marketingdb_domain_model_firma.partner',
'config' => array(
'type' => 'select',
'size' => 5,
'foreign_table' => 'tx_toco3marketingdb_domain_model_partner',
'foreign_table_where' => 'ORDER BY tx_toco3marketingdb_domain_model_partner.partnerpkey',
'minitems' => 0,
'maxitems' => 20,
),
),

Model:

/**
* Partner
*
* @var Tx_Extbase_Persistence_ObjectStorage<Tx_Toco3Marketingdb_Domain_Model_Partner>
* @lazy
*/
protected $partner;
/**
* Sets the partner
*
* @param Tx_Extbase_Persistence_ObjectStorage<Tx_Toco3Marketingdb_Domain_Model_Partner> $partner
* @return void
*/
public function setPartner(Tx_Extbase_Persistence_ObjectStorage $partner) {
$this->partner = $partner;
}

Controller:

$partner = new Tx_Extbase_Persistence_ObjectStorage();
if (count($partnerarr) > 0){
foreach($partnerarr as $p){
$partner->attach( $this->partnerRepository->findByUid($p));
}
}
$organisation = $this->organisationRepository->findByUid($uid)
$organisation->setPartner($partner);

This is working as long there is an Object in the ObjectStorage. So I can add/delete/change relations. But when $partnerarr is empty an no objects get attached an empty Tx_Extbase_Persistence_ObjectStorage is assigned, the old values do not get "deleted". I also tried to assign null or "" but the an error occures because an ObjectStorage is needed. If I assign the empty ObjectStorage I don't get an error, but the old values still maintain :(

Any idea?

Thank you

Christian

2

2 Answers

1
votes

Call the detach or removeAll methods to remove certain or all objects of the storage.

/** @var \Tx_Extbase_Persistence_ObjectStorage $organisationPartners */
$organisationPartners = $organisation->getPartner();
foreach ($organisationPartners as $partner) {
   $organisationPartners->detach($partner);
}
0
votes

Thank you @Wolfgang for your message.

I added the following function to my model:

/**
* detach Partner
*
* @param Tx_Toco3Marketingdb_Domain_Model_Partner $partner
* @return void
*/
public function detachPartner($partner) {
   $this->partner->detach($partner);
} 

In the controller I added:

$persistanceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
 $organisation = $this->firmaRepository->findByUid($uid);

$organisationPartners = $organisation->getPartner();
 foreach ($organisationPartners as $organisationPartner) {
     $organisation->detachPartner($organisationPartner);
 }
 $persistanceManager->persistAll();

 $organisation->setPartner($partner);

It is important to persist before setting the new (empty) value...