[Flow] Custom Factory
Mathis Hoffmann
mathis at hoffpost.de
Thu Jun 27 12:12:43 CEST 2013
Hi,
I'm just trying to use my own factory for creating my objects. I added
the following to Objects.yaml:
HdM\Accounting\Domain\Payment\Category\Model\PaymentCategory:
properties:
cache:
object:
factoryObjectName:
HdM\Accounting\Domain\Payment\Category\Model\PaymentCategoryFactory
factoryMethodName: createPaymentCategory
and created the corresponding FactoryObject. PaymentCategory is an
interface-type which has some concrete subtypes. Depending on the user
input my factory decides which concrete subtype to use. I created a
fluid form with name="newPaymentCategory" and added the two parameters
my factory requires (name:string and type:string) as text-input-fields
using property="type"/property="name" to that form. The createAction
requires newPaymentCategory as type PaymentCategory. I think flow should
now recognise that there is a custom factory for type PaymentCategory
and use my factory but it doesn't. It tries to property-map itself and
complains that PaymentCategory does not have a setter for argument
"type" (which is true because this argument is for my custom factory to
determine the concrete implementation of PaymentCategory it should return).
My factory-method looks like the following:
/**
* @param string $type 'incoming' or 'outgoing'
* @param string $name
* @return
\HdM\Accounting\Domain\Payment\Category\Model\PaymentCategory
* @throws \InvalidArgumentException
*/
public function createPaymentCategory ($type, $name) {
switch ($type) {
case 'incoming':
$c = new IncomingPaymentCategory();
break;
case 'outgoing':
$c = new OutgoingPaymentCategory();
break;
default:
throw new \InvalidArgumentException('Invalid argument
for $type');
}
$c->setName($name);
return $c;
}
And finally the createAction is as simple as follows:
/**
* Adds the given new payment category object to the payment
category repository
*
* @param
\HdM\Accounting\Domain\Payment\Category\Model\PaymentCategory
$newPaymentCategory
* @return void
*/
public function createAction(PaymentCategory $newPaymentCategory) {
$this->paymentCategoryRepository->add($newPaymentCategory);
$this->addFlashMessage('Kategorie angelegt.');
$this->redirect('index');
}
Can anyone tell me how to force Flow to use my custom factory instead of
it's build-in property-mapper which is not really suitable for
polymorphic types?
Cheers
Mathis
More information about the Flow
mailing list