[FLOW3-general] Storing/Receiving Different Models with one Action
Robert Lemke
robert at typo3.org
Wed Jan 6 12:17:58 CET 2010
Hi Marc,
Am 23.12.2009 um 14:29 schrieb marc neuhaus:
> I'm currently diving into FLOW3 and love it!
I'm glad to hear that!
> I have a Controller with an action to create a New Model entity.
> The Problem is, that the Class of this Model varies and is specified through
> the request.
>
> Here is a little code example of what i'm trying to accomplish:
>
> /**
> * Stores the Model
> *
> * @param object $object Should be able To Receive any Kind of Model Object
> and initialize it
> * @return void
> */
> public function createAction(object $object) {
> $modelClass = $this->request->getArgument("model");
>
> $repository = getRepositoryForModel($modelClass);
> $repository->add($object);
> }
>
> Could you give me a hint how i could accomplish something like this?
This is not directly supported by FLOW3 because if FLOW3 doesn't know which object
type is expected in the createAction, it can't map the incoming POST array to this object
and it also can't validate the result before passing it to the action.
You should also have some very good reasons to create such a generic create action
because you'll have a harder time later defining access control policies for it if it has
such a general purpose.
However, it is possible (of course ;-), you only need to take care of a few things yourself.
/**
* @param string $objectType
* @param array $objectProperties
* @return void
*/
public function createAction($objectType, array $objectProperties) {
switch ($objectType) {
case 'F3\MyPackage\Domain\Model\MyModel':
$repositoryObjectName = 'F3\MyPackage\Domain\Repository\MyRepository';
break;
...
default:
throw new SomeException('This object type is not supported');
}
$targetObject = $this->objectFactory->create($objectType);
$this->propertyMapper->mapAndValidate(array_keys($objectProperties), $objectProperties, $targetObject);
$repository = $this->objectManager->getObject($repositoryObjectName);
$repository->add($targetObject);
...
}
Note: I didn't test the above code and I recommend that you double check that you really want this.
Good luck!
Robert
--
Robert Lemke
Fluent Code Artisan
http://typo3.org
http://flow3.typo3.org
More information about the FLOW3-general
mailing list