从Url获取参数到Yii2模型

busg9geu  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(170)

我有问题从URL参数到我的Yii 2模型。
网址:https://example.com/?key=test&id=1234
我的代码是:

public function save()
    {
        $httpClient = new Client();
        $keyword = Yii::$app->getRequest()->getQueryParam('key');
        $data = [
            'civilite'     => $this->civility,
            'nom'          => $this->lastName,
            'prenom'       => $this->firstName,
            'telephone'    => $this->phoneNumber,
            'email'        => $this->emailAddress,
            'operateur'    => $this->operator,
            'tel_domicile' => $this->phone,
            'keyword' => $keyword,
        ];

        $preferences = explode(',', $this->preferences);
        $index = 0;
        foreach ($preferences as $preference) {
            $index++;
            $data['attente' . $index] = $preference;
        }

        LeadLogHelper::log($data);
        $rawResponse = $httpClient->createRequest()
            ->setMethod('POST')
            ->setUrl(\Yii::$app->params['leadWebserviceUrl'])
            ->setData($data)
            ->send();
        $response = json_decode($rawResponse->content);

        if (!$response->Statut) {
            Yii::error('An error occurred while saving the data using the webservice', __METHOD__);
            Yii::error($data, __METHOD__);
            Yii::error($response, __METHOD__);
        }
        return $response->Statut == 1 || $response->Message === 'La Fiche existe déjà.';

    }

保存功能正常工作,但$Keyword的值为空,请帮助!!

tmb3ates

tmb3ates1#

取决于您的查询链接,如:https://example.com/?key=test&id=1234
在调用$model->save()方法时,特别是对于此特定模型,可以传递一个附加参数作为$key,如下所示:

方法1

//action controller
//Your model
 $model->save(\Yii::$app->request->get('key'))

以下是模型:

public function save($key = '')
    {
        $httpClient = new Client();
        $data = [
            'civilite'     => $this->civility,
            'nom'          => $this->lastName,
            'prenom'       => $this->firstName,
            'telephone'    => $this->phoneNumber,
            'email'        => $this->emailAddress,
            'operateur'    => $this->operator,
            'tel_domicile' => $this->phone,
            'keyword' => $key, // Or use if statement to include this value or not
        ];
        ...
    }

但是,使用如下模型属性是安全的:

方法2

//define a property
class YOUR_MODEL extends Model {
    ...
    public $key;
    ...
    public function rules()
    {
        return [
            ...
            [['key'], 'safe'],
            ...
        ];
    }
}

然后你可以在控制器中使用这个:

$model->key = \Yii::$app->request->get('key');

在您的模型中进行变更:

public function save()
    {
        $httpClient = new Client();
        $data = [
            'civilite'     => $this->civility,
            'nom'          => $this->lastName,
            'prenom'       => $this->firstName,
            'telephone'    => $this->phoneNumber,
            'email'        => $this->emailAddress,
            'operateur'    => $this->operator,
            'tel_domicile' => $this->phone,
            'keyword' => $this->key
        ];

        ...
    }

然后调用$model->save()方法。
希望这对你有帮助。

相关问题