简体   繁体   中英

Yii, use one textbox to update multiple models

I hate to ask such a general question, and this is not a write the code for me question. I just need some help getting started in the right direction.

I'm going to be doing something similar to this: http://www.yiiframework.com/doc/guide/1.1/en/form.table

But here is the scenario, my models are lumber products. You can have a 2x4 8' as one and 2x4 10' as another. All the way up to 24'. They all have a price field, with a different price. BUT they can be priced by the lineal foot, board foot, thousand board foot.. etc.

So a 2x4 8' price may be 1.92 and 2x4 10' price may be 2.40. But you can set the value of prices by passing.24 to both models as the lineal price. So.24 * 8 and.24 * 10. Each lumber model has field called length that can be used here.

Using Yii, and doing it the Yii way... not some work around way like I would think of. What would be the right way to go about linking one CActiveTextField to multiple models. Eg. a group of 2x4 items.

My view with the form would end up looking something like this:

    Fir   Spruce
2x4  [.24] [ .22]
2x6  [.30] [ .32]

Inbetween [ ] are the input boxes that are linked to multiple models... so the top left would be linked to all 2x4s that are fir

Thanks =p

I don't know the correct 'YII way' but you can update several models from your controller like so:

   /**
     * Update several models.
     */
    public function actionUpdate()
    {
            // post filtering left out for brevity
            $editFir10 = new Fir10;
            $editFir8 = new Fir8;
            $editFir10->costPerLF=$_POST['firCLF'];
            $editFir8->costPerLF=$_POST['firCLF'];
            $editFir10->save();
            $editFir8->save();
            //here you can send several models AFAIK
            $this->render('update',array(
                    'model1'=>$editFir8,
                    'model2'=>$editFir10,
            ));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM