[FLOW3-general] Automatically add an object possible?

Robert Lemke robert at typo3.org
Tue Oct 26 22:30:56 CEST 2010


Hi Mario,

Am 26.10.2010 um 21:38 schrieb Mario Rimann:

> I'm working on adding a parent and a child object. Adding the parent
> object works with the newAction() and createAction() as in the Blog
> example. Adding the child object works fine, too (via the
> childController's new/createActions).
> 
> But what I'd like to get, is that the parentController's createAction()
> does not just add the new object to the repository, but also
> automatically adds a corresponding childObject right away. I tried to
> achieve this by the following createAction() in the parentController -
> but it just throws me the following PHP error message (no Exception!):
> 
> Fatal error: Call to undefined method
> F3\Cockpit\Domain\Model\Domain::FLOW3_AOP_Proxy_getProxyTargetClassName() in
> /var/www/rimann.org/cockpit/Packages/Framework/FLOW3/Classes/Persistence/Backend/GenericPdo/Backend.php
> on line 200

This error occurs because you instantiated the Child class with the new operator.
In FLOW3 you must (almost) always use the object manager for creating instances
(see below).

However, a more friendly error message would be nice - therefore, would you
please open an issue in the FLOW3 project so we don't forget to add an addtional
check?

> public function createAction(\F3\MyPackage\Domain\Model\Parent $newParent) {
> 	$this->parentRepository->add($newParent);
> 		
> 	$newChild = new \F3\MyPackage\Domain\Model\Child();
> 	$newChild->setParent($newParent);
> 	$newChild->setName($newParent->getName());
> 	$this->childRepository->add($newChild);
> 	$newParent->addChild($newChild);
> 
> 	$this->flashMessageContainer->add('Your new parent was created.');
> 
> //	$this->redirect('index');
> }

Try this:

$this->parentRepository->add($newParent);
$newChild =  $this->objectManager->create('F3\MyPackage\Domain\Model\Child');
$newParent->addChild($newChild);

you don't need to add the child to the ChildRepository explicitly because it's
already connected via the parent.

Cheers,
Robert


More information about the FLOW3-general mailing list