[FLOW3-general] War with OneToMany and many workarounds (authors of FLOW3 pls reply)

Dawid Pacholczyk dpacholczyk at gmail.com
Sun Sep 4 21:22:37 CEST 2011


Hello List,
  I would like to discuss about @OneToMany relation. As you can see in 
my previous topics I had BIG problems with it. I would like you to see 
what I had to do to make it works. I mean that by one form I could add 
new user with address and the address with connection to User

First the initializeCreateAction

	/**
	 * Initialize createAction
	 *
	 * @return void
	 */
	public function initializeCreateAction() {
	 
$this->arguments['newUser']->getPropertyMappingConfiguration()->forProperty('birthdate')->setTypeConverterOption('TYPO3\FLOW3\Property\TypeConverter\DateTimeConverter',
 
\TYPO3\FLOW3\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, 
'd-m-Y');

 
$this->arguments['newUser']->getPropertyMappingConfiguration()->forProperty('addresses')->setTypeConverterOption('TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter', 

				 
\TYPO3\FLOW3\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, 
true);
        # 
$this->arguments['newUser']->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('addresses', 
'SFTAG\Core\Domain\Model\Address');
	 
$this->arguments['newUser']->getPropertyMappingConfiguration()->allowCreationForSubProperty('addresses');
	 
$this->arguments['newUser']->getPropertyMappingConfiguration()->allowCreationForSubProperty('addresses.0');
	}

I had to change allowModificationForSubProperty from the examples and my 
discussion to allowCreationFroSubProperty. It worked but when I created 
both objects Address had no relation to User in db. Null was in this field.

Now my createAction


	/**
	 * Adds the given new user object to the user repository
	 *
	 * @param \SFTAG\Core\Domain\Model\User $user A new user to add
	 */
	public function createAction(User $newUser) {
		$addresses = $newUser->getAddresses()->toArray();
		$addresses[0]->setUser($newUser);
		$newUser->setAddresses(null);
		
		$newUser->addAddress($addresses[0]);
		
		$this->userRepository->add($newUser);
	}


Look here. I had to grab addresses (collection) from $newUser. Convert 
it array and grab first object. then i had to clear the collection than 
add to the object asociated with it User. Update object of type Address 
I`ve added to the user addresses collection and finally save the user in DB

Now the addAddress in my User model

	/**
	 * Adds address to user
	 *
	 * @param \SFTAG\Core\Domain\Model\Address $address
	 * @return void
	 */
	public function addAddress(\SFTAG\Core\Domain\Model\Address $address) {
		$address->setUser($this);
		
		if($this->addresses == null)
			$this->addresses = new ArrayCollection();
			
		$this->addresses->add($address);
	}

See what I had to do ? I had to check first is $this->addresses is null. 
You could say that I should initialize this in my controller

	public function __construct() {
// I use only ArrayCollection after new cause I used "use 
Doctrine\Common\Collections\ArrayCollection;"
		$this->addresses = new ArrayCollection();
	}

I did. Like in blog example. And still I need to check if its null

After all this workaround I could add User with Address asociated. I 
worked on that for about 6 days. It was rly frustrating time.


One more question. What is with method getPropertyMappingConfiguration() 
? It`s not in official API for Argument or Arguments but via google I 
can find old version where this method exists. So is there or not ? If 
not why should I use it ?

I hope that authors will join my in this topic cause this fight of mine 
was rly strange. I know that it can be done better but there is no clue 
how it is possible. Or maybe it isn`t ?

Please verify this post and answer something. Don`t get it as and 
message from angry developer that is too stupid too understand your 
work. Just look how long was my discussion with Ferdinand (title: 
Mapping and adding objects - problem with properties). I rly hope will 
have a nice and educational discussion.

best regards,
Dawid Pacholczyk


More information about the FLOW3-general mailing list