Sunday, January 2, 2022

[FIXED] Get Parametrs from Url to Yii2 model

Issue

I have issue with getting parameter from URL to my Yii2 Model.

URL : https://example.com/?key=test&id=1234

My code is :

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à.';

    }

The Save function work, but with a null value for $Keyword, please Help!!


Solution

Depend on your query link like: https://example.com/?key=test&id=1234

At the time when you call $model->save() method, special for this particular model you can pass an additional parameter as $key like this:

Method 1

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

Here is the model:

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
        ];
        ...
    }

But it is safe to use model properties like this:

Method 2

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

Then you can use this in controller:

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

In your model make changes:

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
        ];

        ...
    }

And after that call $model->save() method.

Hope this helps.



Answered By - Serghei Leonenco

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.