锂电从视图中填充多个模型 [英] lithium fill multiple models from view

查看:81
本文介绍了锂电从视图中填充多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以可以说我有一个实体模型,它是人员和组织模型的基础.

So lets say I have an Entities Model which is the base for a People and Organizations Model.

我有三个空集合,一个用于实体,一个用于人员,一个用于组织.

I've three empty collections, one for Entities, one for People, one for Organization.

让我们假设,出于这个问题的目的,人与组织之间的关系将被忽略.

Lets assume that the relationships between People and Organization will be ignored for the purpose of this question.

最后,我有一个视图,其中包含所有三个模型的字段.

Finally, I have a view, with fields for all three models.

我的问题:我是否创建模型类作为DTO(数据传输对象)只是为了保存所有三个模型的数据并将它们保存到控制器中各自的集合中?

My question: Do I create a model class as a DTO (data transfer object) just to hold the data for all three models and save them to their respective collections in the controller?

DTO层次结构如下:

The DTO hierarchy would be like this:

  • AddClientDTO
    • 实体(带有字段)
      • 组织(包含字段)
      • 人员(带有字段)
      • AddClientDTO
        • Entity (with fields)
          • Organization (with fields)
          • Person (with fields)

          推荐答案

          听起来您正在尝试使用单个表单和控制器操作为3种不同模型创建记录.这是一种至少应该使您入门的简单方法.

          It sounds like you're trying to create records for 3 different models using a single form and controller action. Here's a simple way to do it that should at least get you started.

          首先,在应用程序/模型中创建实体,人员和组织模型.

          First, create Entities, People, and Organization models in app/models.

          然后,创建一个包含以下操作的实体控制器:

          Then, create an entities controller containing the following action:

          public function add() {
             $entity = Entities::create(); 
             if($this->request->data && $entity->save($this->request->data)) {
                echo 'Success!';
                exit;
             }
             return compact('entity');
          }
          

          接下来,创建/app/views/entities/add.html.php:

          Next, create /app/views/entities/add.html.php:

          <?=$this->form->create($entity); ?>
          <?=$this->form->label('name', 'Entity Name');?>
          <?=$this->form->text('name');?>
          <?=$this->form->label('person_data[name]', 'Person Name');?>
          <?=$this->form->text('person_data[name]');?>
          <?=$this->form->label('organization_data[title]', 'Organization Title');?>
          <?=$this->form->text('organization_data[title]');?>
          <?=$this->form->submit('Save');?>
          <?=$this->form->end(); ?>
          

          最后,描述您的关系并在app/models/Entities.php中添加一个过滤器,以在保存数据时保存人员和组织:

          Finally, describe your relationships and add a filter in app/models/Entities.php to save the person and organization when the data is saved:

          <?php
          
          namespace app\models;
          use \app\models\People;
          use \app\models\Organizations;
          
          class Entities extends \lithium\data\Model {
          
              public $belongsTo = array(
                  'Organizations' => array(
                      'class' => '\app\models\Organizations',
                      'key' => 'organization_id',
                  ),
                  'People' => array(
                      'class' => '\app\models\People',
                      'key' => 'person_id',
                  ),
              );
          
              public static function __init() {
                  parent::__init();
          
                  static::applyFilter('save', function($self, $params, $chain) {
          
                      // If data is passed to the save function, set it in the record
                      if ($params['data']) {
                          $params['entity']->set($params['data']);
                         $params['data'] = array();
                      }
          
                      $record = $params['entity'];
          
                      if(isset($record->person_data)) {
                          $person = People::create($record->person_data);
                          if($person->save()) {
                              $record->person_id = $person->id;
                          }
                      }
          
                      if(isset($record->organization_data)) {
                          $org = Organizations::create($record->organization_data);
                          if($org->save()) {
                              $record->organization_id = $org->id;
                          }
                      }
          
                      $params['entity'] = $record;
                      return $chain->next($self, $params, $chain);
          
                  });
              }
          
          }
          
          ?>
          

          如果我对您要尝试的操作做了太多假设,请留下评论,我将相应地调整代码.希望这会有所帮助!

          If I made too many assumptions about what you're trying do to, leave a comment and I'll adjust the code accordingly. Hope this helps!

          这篇关于锂电从视图中填充多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆