前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Yii2数据

Yii2数据

作者头像
botkenni
发布2022-01-10 10:45:46
6290
发布2022-01-10 10:45:46
举报
文章被收录于专栏:IT码农

添加表单使用了数据模型xxModel没有的字段,需要新建表单模型xxForm,这个时候,在控制器接受到了数据,子表单模型使用表单接受到的数据进行拼接,如:$this->product

代码语言:javascript
复制
public function validateItems()
{
    $productIdList = $this->items['product_id'];
    $qtyList = $this->items['qty'];
    $productPriceIds = $this->items['product_price_id'];

    foreach ($productIdList as $i => $product_id)
    {
        $price = null;
        $qty = $qtyList[$i];
        $product_price_id = $productPriceIds[$i];
        if(isset($product_price_id) && isset($qty) && $qty > 0)
        {
            $product = Product::findOne($product_id);

            if(null != $product && $product->isOnline())
            {
                if($product->isAreaPrice())
                {

                    if(!isset($product_price_id))
                    {
                        var_dump(000);die;
                        $this->addError('items', '编号为'.$i.'的商品地区未选择。');
                    }
                    else
                    {
                        /** @var ProductPrice $pp */
                        $pp = ProductPrice::find()->where(['id' => $product_price_id, 'product_id' => $product_id])->one();
                        if(null != $pp)
                        {
                            $price = $pp->price;
                        }
                    }
                }
                else
                {
                    $price = $product->price;
                }
                $this->products[] = [
                    'product' => $product,
                    'qty' => (int)$qty,
                    'price' => $price
                ];
            }
            else
            {
                $this->addError('items', '编号为'.$i.'的商品信息不正确。');
            }
        }
        else
        {
            $this->addError('items', '编号为'.$i.'的商品信息不正确。');
        }
    }
}

保存时数据

代码语言:javascript
复制
public function save()
{
    $order = new Order();
    $this->products;

    if(!$order->save(false)) return null;
    return $order;
}

控制器

代码语言:javascript
复制
/*
 * 新增订单
 */
public function actionCreate($user_id)
{
    $user = $this->findUserModel($user_id);
    $model = new OrderForm();
    $model->user = $user;
    $model->user_id = $user_id;
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $order = $model->save();
        if($order){
            Yii::$app->session->setFlash('success', '订单保存成功!');
            return $this->redirect(['list']);
        }
        Yii::$app->session->setFlash('error', '保存失败。');
    }
    if ($model->hasErrors()) {
        Yii::$app->session->setFlash('error', '保存失败, 您的订单填写有误, 请检查。');
    }
    return $this->render('create', [
        'model' => $model,
        'user' => $user,
    ]);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/03/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档