Php Yii2 rest保存多个模型

Php Yii2 rest保存多个模型,php,rest,yii2,Php,Rest,Yii2,使用REST方法,我希望能够在单个操作中保存多个模型 class MyController extends ActiveController { public $modelClass = 'models\MyModel'; } class MyModel extends ActiveRecord { ... } 自动为RESTAPI创建操作。问题是,我想保存多个模型,在帖子中仅使用该代码将仅为MyModel生成一条新记录。如果我需要保存一个Thermodel呢 谢谢您的建议。因此

使用REST方法,我希望能够在单个操作中保存多个模型

class MyController extends ActiveController {
    public $modelClass = 'models\MyModel';
}

class MyModel extends ActiveRecord { 
 ...
}
自动为RESTAPI创建操作。问题是,我想保存多个模型,在帖子中仅使用该代码将仅为MyModel生成一条新记录。如果我需要保存一个Thermodel呢


谢谢您的建议。

因此,如果我知道您不仅希望为正在查询的模型添加新的数据库条目,还希望为另一个模型添加新的数据库条目


执行此操作的最佳位置是第一个模型类的AfterSave()或BeforeSave()函数。哪一个取决于您正在保存的数据。

ActiveController实现了一组通用的基本操作,以支持对ActiveRecord的RESTful访问。对于更高级的使用,您需要覆盖它们,或者只是将您自己的自定义操作合并到它们中,您将在其中实现您自己的代码和逻辑

检查应用程序中的
/vendor/yiisoft/yii2/rest/
文件夹,查看ActiveController是如何构造的,以及它的每个操作都在做什么

现在,要通过自定义操作覆盖ActiveController的操作开始,您可以在控制器中执行该操作。下面是我覆盖createAction的第一个示例:

1-

2-

或者,您可以通过将自定义操作放在单独的文件中,遵循
/vendor/yiisoft/yii2/rest/ActiveController.php
中的ActiveController结构。下面是一个示例,在这个示例中,我用一个自定义操作覆盖了updateAction,我从myController类初始化它的参数:

class MyController extends ActiveController
{
    public $modelClass = 'models\MyModel';

    public function actions() {

        $actions = parent::actions();

        $custom_actions = [
            'update' => [
                'class' => 'app\controllers\actions\WhateverAction',
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'scenario' => $this->updateScenario,
                'params'      => \Yii::$app->request->bodyParams,
            ],
        ];

        return array_merge($actions, $custom_actions);
    }
}
现在让我们举个例子,在我的新操作文件
app\controllers\actions\WhateverAction.php
中,我期待着Post请求(我存储在
$params
中)要使子模型具有存储子模型列表的属性,我将应用一些额外的代码,如将它们与其父模型关联(如果它们首先已经存在):

namespace app\controllers\actions;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\ServerErrorHttpException;
use yii\rest\Action;
use app\models\YourSubModel;

class WhateverAction extends Action
{

    public $scenario = Model::SCENARIO_DEFAULT;
    public $params;

    public function run($id)
    {
        $model = $this->findModel($id);

        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id, $model);
        }

        $model->scenario = $this->scenario;
        $model->load($this->params, '');

        foreach ($this->params["subModels"] as $subModel) {
          /**
            * your code related to each of your model's posted child
            * for example those lines will relate each child model
            * to the parent model by saving that to database as their
            * relationship has been defined in their respective models (many_to_many or one_to_many)
            *
            **/
            $subModel = YourSubModel::findOne($subModel['id']);
            if (!$subModel) throw new ServerErrorHttpException('Failed to update due to unknown related objects.');
            $subModel->link('myParentModelName', $model);
            //...
        }

        // ...

        return $model;
    }
}
namespace app\controllers\actions;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\ServerErrorHttpException;
use yii\rest\Action;
use app\models\YourSubModel;

class WhateverAction extends Action
{

    public $scenario = Model::SCENARIO_DEFAULT;
    public $params;

    public function run($id)
    {
        $model = $this->findModel($id);

        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id, $model);
        }

        $model->scenario = $this->scenario;
        $model->load($this->params, '');

        foreach ($this->params["subModels"] as $subModel) {
          /**
            * your code related to each of your model's posted child
            * for example those lines will relate each child model
            * to the parent model by saving that to database as their
            * relationship has been defined in their respective models (many_to_many or one_to_many)
            *
            **/
            $subModel = YourSubModel::findOne($subModel['id']);
            if (!$subModel) throw new ServerErrorHttpException('Failed to update due to unknown related objects.');
            $subModel->link('myParentModelName', $model);
            //...
        }

        // ...

        return $model;
    }
}