douqin0676 2012-08-01 12:34
浏览 16
已采纳

控制器使用ATK4导入不同格式的供应商数据

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?

  • 写回答

1条回答 默认 最新

  • download12749 2012-08-02 09:56
    关注

    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();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系