2

I like to understand conceptually how to setup controller / model in case I want to import supplier data (csv/xml/soap/..) via different controllers. I simplified my case for better focus.

In ATK4 I have Model_Supplier. The main fields are: name,type. Type can be csv1,csv2,csv3,xml1,xml2,soap1. Some suppliers are using the same type.

I like to load the model and do ->import(). However import() should be different for each type.

I thought about following possibilities: [1] load model, setController, hook method to model and then use model->import()

$m=$this->add('Model_Supplier)->load(1);
$m->setController($m['type']);
$m->import();

then in Controller_Csv1 the method import() needs to be added like this via the init():

$this->owner->addMethod('import',array($this,'import'));

[2] load model, setController and do controller->import()

$m=$this->add('Model_Supplier)->load(1);
$c=$m->setController($m['type']);
$c->import();

Then the import() in the controller should refer to $this->owner as the model

[3] Another option would be to extend Model_Supplier with Model_Supplier_Csv1 and then have import() in here. However then I first need to load Model_Supplier to identify the type, then unload and load again Model_Supplier_$type

[4] Or add model, then add controller, then set the model instance to the controller and do controller->import()

$m=$this->add('Model_Supplier)->load(1);
$c=$this->add('Controller_'.$m['type']);
$c->setModel($m);
$c->import();

[5] maybe another solution which I didn't think of.

What would be the best approach also in line with ATK4?

Bob Siefkes
  • 1,133
  • 9
  • 11

1 Answers1

2

2.

This varies depending on many factors in your application, but I think second option is most suitable. The usage would be:

$model->setController($m['type'])->import();

You can also create a method in your model:

function import(){
    return $this->setController($m['type'])->import();
}

then you can simply call $model->import();

romaninsh
  • 10,606
  • 4
  • 50
  • 70