lithium fill multiple models from view

后端 未结 2 1491
醉话见心
醉话见心 2021-01-07 14:18

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

2条回答
  •  情话喂你
    2021-01-07 15:25

    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');
    }
    

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

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

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

     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!

提交回复
热议问题