[TYPO3-mvc] Need help with recursive domain objects

Tim Schoch | GSTALTIG tim.schoch at gstaltig.ch
Mon Oct 11 17:05:00 CEST 2010


Hello List
 
I'm having a hard time updating my recursive domain objects. Switch a sub-object from one parent object to another one will result in a strange behavior.
So far I've compared my code to the kickstarter and the mittwald_timetrack extension but couldn't find my bug.
 
--[ EXAMPLE ]----------------------------------------------------
 
Starting Position:
Block A ( children: Block B, Block C )
- Block B
- Block C
 
Now I change the parent Block of Block C.
Expected Output:
Block A ( Block B )
- Block B ( Block C )
- - Block C
 
The assignement with the NEW Parent Block works as a charm, but the old parent Block won't get updated.
Output will be:
Block A ( none ) 
Block B ( Block C )
- Block C
 
I've tracked it down to the removeBlock() Function:
/**
* @param Tx_Ext_Domain_Model_Block The Block to be removed
* @return void
*/
public function removeBlock ( Tx_Ext_Domain_Model_Block $block ) {
  $this->block->detach( $block );
}
After the detach function is called, $this->block still holds the block I want to remove.
My guess is this: First I call detach, which fails. Then attach() is called on the new parent object and succeeds. Now two relations are stored to the same object and extbase flushes the old one (with all the other children). I'm not 100% sure, because I haven't got a real debugger at hand.
 
What am I doing wrong?
 
--[ CODE SNIPETS ]----------------------------------------------------
 
ext_tables.sql
---------------------------------
.
parent_block    int(11) unsigned DEFAULT '0',
block                      int(11) unsigned DEFAULT '0',
.
 
tca.php
---------------------------------
// parent block
'parent_block' => array(
  'config' => array(
    'type' => 'passthrough',
  )
),
// child blocks
'block' => array(
  'config'  => array(
    'type' => 'inline',
    'foreign_table' => 'tx_Ext_domain_model_block',
    'foreign_field' => 'block',
    'maxitems'      => 99999,
  )
),
 
Block.php ( Domain Model )
---------------------------------
/**
* child blocks
* @var Tx_Extbase_Persistence_ObjectStorage<Tx_Ext_Domain_Model_Block>
* @lazy
* @cascade remove
*/
protected $block;
 
/**
* parentBlock
* @var Tx_Ext_Domain_Model_Block
*/
protected $parentBlock = NULL;
 
public function __construct() {
  $this->block = new Tx_Extbase_Persistence_ObjectStorage();
}
 
/**
* Adds a Block
*
* @param Tx_Ext_Domain_Model_Block The Block to be added
* @return void
*/
public function addBlock ( Tx_Ext_Domain_Model_Block $block ) {
  $block->setParentBlock( $this );
  $this->block->attach( $block );
}
/**
* Removes a Block
*
* @param Tx_Ext_Domain_Model_Block The Block to be removed
* @return void
*/
public function removeBlock ( Tx_Ext_Domain_Model_Block $block ) {
  $this->block->detach( $block );
}
 
BlockController.php
---------------------------------
/**
* @param Tx_Ext_Domain_Model_Block $block The Block to display
* @param Tx_Ext_Domain_Model_Block $previousParentBlock The previous ParentBlock, declared in the fluid template
*/
public function updateAction( Tx_Ext_Domain_Model_Block $block, Tx_Ext_Domain_Model_Block $previousParentBlock = NULL ) {
  // if the parent has changed, remove from previous parent
  if ( $block->getParentBlock() !== $previousParentBlock && $previousParentBlock instanceof Tx_Ext_Domain_Model_Block ) {
    $previousParentBlock ->removeBlock( $block );
  }
 
  // add to and save parent block if one is selected
  if ( $block->getParentBlock() instanceof Tx_Ext_Domain_Model_Block ) {
    $block->getParentBlock()->addBlock( $block );
    $this->blockRepository->update( $block->getParentBlock() );
  } 
  // no parent block selected
  else {
    $this->blockRepository->update( $block );
  }
}
 
Thank you very much
 
Gruess Tim
 
GSTALTIG GMBH / www.gstaltig.ch
Sonnenhofstr. 13 / 6020 Emmenbrücke
tim at gstaltig.ch / 076 488 24 97
 


More information about the TYPO3-project-typo3v4mvc mailing list