【问题标题】:How to use multiple tables in one model in yii2yii2如何在一个模型中使用多个表
【发布时间】:2016-08-01 01:08:10
【问题描述】:

我有一个产品模型,可以将信息保存到数据库中的产品表中,但我的数据库中也有价格表、颜色表和尺寸表,在表格上我将通过以下方式获取所有产品信息,包括价格、尺寸和颜色产品控制器和产品型号,现在我想知道如何在表格中以不同的方式保存价格、尺寸和颜色。下面是截图

public function actionCreate(){
$data = \Yii::$app->request->post();
$model = new Product();
$model->title = $data['title'];
$model->name = $data['name'];
}

现在我怎样才能将此表名称更改为价格或大小或颜色,以便能够将$data['size'] and $data['color'] and $data['price'] 保存到相应的列

【问题讨论】:

  • 从你的问题标题How to use... 答案是使用use :)

标签: php mysql yii2


【解决方案1】:

一个模型与一个数据库表相关联。

关于处理不同类型的多个模型,官方文档中有一篇很好的文章-Getting Data for Multiple Models

省略细节,这里是控制器的代码sn-p:

namespace app\controllers;

use Yii;
use yii\base\Model;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use app\models\User;
use app\models\Profile;

class UserController extends Controller
{
    public function actionUpdate($id)
    {
        $user = User::findOne($id);
        if (!$user) {
            throw new NotFoundHttpException("The user was not found.");
        }

        $profile = Profile::findOne($user->profile_id);

        if (!$profile) {
            throw new NotFoundHttpException("The user has no profile.");
        }

        $user->scenario = 'update';
        $profile->scenario = 'update';

        if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) {
            $isValid = $user->validate();
            $isValid = $profile->validate() && $isValid;
            if ($isValid) {
                $user->save(false);
                $profile->save(false);
                return $this->redirect(['user/view', 'id' => $id]);
            }
        }

        return $this->render('update', [
            'user' => $user,
            'profile' => $profile,
        ]);
    }
}

为了查看:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'user-update-form',
    'options' => ['class' => 'form-horizontal'],
]) ?>
    <?= $form->field($user, 'username') ?>

    ...other input fields...

    <?= $form->field($profile, 'website') ?>

    <?= Html::submitButton('Update', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end() ?>

这篇文章也可能有用 - Collecting tabular input。它涵盖了从多个相同类型的模型收集数据。

另请阅读Models 部分,尤其是验证规则大量分配 段落。您应该避免处理 $_POST 这样的参数。

【讨论】:

    【解决方案2】:

    您应该为每个表建立一个模型。在价格、颜色和尺寸表中插入产品的 ID。在所有其他表中添加 product_id。试试这个:

        public function actionCreate()
    {
        $data = \Yii::$app->request->post();
        $model = new Product();
        $model->title = $data['title'];
        $model->name = $data['name'];
        $model->save();
        $getlast=Yii::$app->db->getLastInsertId();
    
        $model = new Price();
        $model->price=Yii::$app->request->post('price');
        $model->product_id = $getlast;
        $model->save();
    
        $model = new Size();
        $model->size=Yii::$app->request->post('size');
        $model->product_id = $getlast;
        $model->save();
    }
    

    【讨论】:

    • 好的,谢谢你们的想法,我知道我可以这样做,但我在想也许没有单独的颜色模型,尺寸也许我可以动态更改表名,但是我认为从我得到的所有答案中这几乎是不可能的,所以我需要为每个表创建一个模型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多