TYPO3 Extbase how to empty ObjectStorage

偶尔善良 提交于 2019-12-04 05:51:06

问题


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


回答1:


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);
}



回答2:


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...



来源:https://stackoverflow.com/questions/40246604/typo3-extbase-how-to-empty-objectstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!