Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling multiple models with a single form #3306

Closed
marciocamello opened this issue Apr 30, 2014 · 12 comments
Closed

Handling multiple models with a single form #3306

marciocamello opened this issue Apr 30, 2014 · 12 comments
Assignees
Labels
type:docs Documentation

Comments

@marciocamello
Copy link
Contributor

Possible use in actionCreate?

public function actionUpdate()
    {
        $settings = Setting::find()->indexBy('id')->all();

        if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
            foreach ($settings as $setting) {
                $setting->save(false);
            }

            return $this->redirect('index');
        }

        return $this->render('update', ['settings' => $settings]);
    }
@qiangxue
Copy link
Member

Please use our forum to ask questions. Thanks.

@marciocamello
Copy link
Contributor Author

This is not a question for the forum, there is a way to use loadMultiple to update, not to create more, should have documentation or create some method in model.php for multiple forms using loadMultiple.

@qiangxue
Copy link
Member

I see. I didn't understand your initial post. Re-opened for doc.

@qiangxue qiangxue reopened this Apr 30, 2014
@qiangxue qiangxue added this to the 2.0 RC milestone Apr 30, 2014
@marciocamello
Copy link
Contributor Author

My example for two methods

public function actionCreate() {

    $model = [];
    $model[] = new Settings();

    $postData = Yii::$app->request->post();
    if ($postData) {
        foreach ($postData as $i => $single) {
            $model[$i] = new Settings();
            }
       }

        if (Model::loadMultiple($model, Yii::$app->request->post())) {

            $validate = false;
            foreach ($model as $index => $setting) {
                /*valitade first form*/
                if($index==0){

                    if($setting->validate($setting->load(Yii::$app->request->post()))){
                        $validate = true;
                    }else{
                         /*return message validation here*/
                    }
                }

                if($validate==true){
                    if($setting->save(false)){
                        $save = true;
                    }
                }
            }

            if($save==true)
            {
                 /*return save message here*/
            }

    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

public function actionUpdate($id)
{
    $model =  Settings::find()->indexBy('id')->all();

        if (Model::loadMultiple($model, Yii::$app->request->post())) {

            $validate = false;
            foreach ($model as $index => $setting) {
                /*valitade first form*/
                if($index==0)
                {
                    if($setting->validate($setting->load(Yii::$app->request->post()))){
                        $validate = true;
                    }else{
                         /*return message validation here*/
                    }
                }

                if($validate==true){
                    if($setting->save(false)){
                        $save = true;
                    }
                }
            }

            if($save==true)
            {
                /*return save message here*/
            }

    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

@cebe
Copy link
Member

cebe commented May 4, 2014

other example: http://www.yiiframework.com/forum/index.php/topic/53935-subforms/page__view__findpost__p__248184

@amjadniazi48
Copy link
Contributor

@marciocamello
Copy link
Contributor Author

This example is good for 2 or more models, great.

@githubjeka
Copy link
Contributor

@samdark samdark closed this as completed Jul 9, 2015
@cebe cebe removed this from the 2.0.x milestone Jul 9, 2015
@sieulog
Copy link

sieulog commented Nov 19, 2015

https://github.com/yiisoft/yii2/blob/master/docs/guide/input-multiple-models.md

This document don't have example with create new.

@StagnantIce
Copy link

What about dinamic count of each model? It is posible?

@cebe
Copy link
Member

cebe commented Dec 4, 2015

@sieulog @StagnantIce please open new issues for this including more detailed description about your case.

@StagnantIce
Copy link

In controller:

public function actionCreate()
{
    $model = new LearnQuestion();
    // http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html
    $count = count(Yii::$app->request->post('LearnQuestionPart', []));
    $partModels = [new LearnQuestionPart()];
    for($i = 1; $i < $count; $i++) {
        $partModels[] = new LearnQuestionPart();
    }
    $post = Yii::$app->request->post();
    if ($model->load($post) && Model::loadMultiple($partModels, $post) && $model->save()) {
        $error = false;
        foreach ($partModels as $partModel) {
            $partModel->question_id = $model->id;
            if (!$partModel->save()) {
                $model->delete();
                $error = true;
                break;
            }
        }
        if (!$error) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
    }
    return $this->render('create', [
        'model' => $model,
        'partModels' => $partModels
    ]);
}

In view:

        <? foreach($partModels as $index => $partModel): ?>
        <tr>
            <td>
                <?= $form->field($partModel, '['.$index.']description')->label(false)->textInput();?>
            </td>
            <td>
                <?= $form->field($partModel, '['.$index.']answer')->label(false)->checkbox([], false);?>
            </td>
        </tr>
        <? endforeach; ?>

$(function(){

$.fn.addRow = function() {
    var $table = $('table tbody', $(this));
    var $tr = $table.find('tr:last');
    var index = $table.find('tr').length - 2;
    var rowHtml = $tr.html().replace(/LearnQuestionPart\[\d+\]/, "LearnQuestionPart[" + (index + 1) + "]");
    $table.append('<tr>' + rowHtml + '</tr>');
}

});

Not user friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

8 participants