G
G
Greg Popov2016-05-21 17:49:33
Yii
Greg Popov, 2016-05-21 17:49:33

How to save a many-to-one set in Yii2?

Hello. So:
1. There is a table Goods
2. There is a table Tech. characteristics
Here is the model of those. characteristics:

class ProductTechSpec extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'product_tech_spec';
    }

    public function attributeLabels()
    {
        return [
            'id' => 'primary key',
            'product_id' => 'primary key',
            'title'      => 'заголовок',
            'spec'       => 'описание',
        ];
    }

    public function getProduct()
    {
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
    }

    public static function find()
    {
        return new ProductTechSpecQuery(get_called_class());
    }
}

Here's what I've been able to gather from the forums:
public function actionCreate()
    {
        $model = ['product' => new Product(), 'product_tech_spec' => [new ProductTechSpec()]];

        $count = count(Yii::$app->request->post('Setting', []));

        for($i = 1; $i < $count; $i++) {
            $model['product_tech_spec'][] = new ProductTechSpec();
        }

        if ($model['product']->load(Yii::$app->request->post()) && $model['product']->save()) {

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

                Model::validateMultiple($model['product_tech_spec'],true);

                foreach ($model['product_tech_spec'] as $tech_spec) {
                    $tech_spec->product_id = $model['product']->id;
                    $tech_spec->save(false);
                }

                return $this->redirect(['view', 'id' => $model['product']->id]);

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

But, there is one “but”:
It saves only the latest technical specification, what is the mistake?
PS How to validate fields generated in view separately for each? For the model, if you fill in one field, all the others automatically become valid.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Donkovtsev, 2016-05-21
@Demetriy

I don’t quite understand why you put an array with the product and its associated models into the $model variable, they don’t teach this in lessons like this, I also recommend using transactions so that nothing is saved in case of errors somewhere, and of course, you in vain disable validation while maintaining the characteristics.
1) Do

$model['product_tech_spec'] = [new ProductTechSpec()];
    for($i = 1; $i < $count; $i++) {
        $model['product_tech_spec'][] = new ProductTechSpec();
    }

instead of
for($i = 1; $i < $count; $i++) {
        $model['product_tech_spec'][] = new ProductTechSpec();
    }

2) There is an error:
The second argument must be removed.
3) Also from two conditions
return $this->render('create', [
                'model' => $model,
            ]);

You can get rid of it by simply removing the two elses and inserting this piece of code at the end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question