[Flow] Custom Factory

Mathis Hoffmann mathis at hoffpost.de
Fri Jun 28 18:23:38 CEST 2013


Hey Alexander,

thank you for your quick reply!

I decided to use version 1 and already archieved a working example.

Thank's!
Mathis

Am 27.06.2013 12:31, schrieb Alexander Berl:
> The object configuration in your case is missing information on where to
> pick the two factory method parameters from and it's not possible to
> configure for using action method arguments.
> I see two possible ways to achieve what you want:
>
> 1) write your own TypeConverter especially for the PaymentCategory type,
> which makes use of the factory
>
> 2) Do the object creation inside the action and only use $type and $name
> as action parameters
>
> Regards,
> Alexander
>
> Am 27.06.2013 12:12, schrieb Mathis Hoffmann:
>> 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
> _______________________________________________
> Flow mailing list
> Flow at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/flow



More information about the Flow mailing list