0

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!!

5
  • 1
    is keyword declared in the rules for your model? Commented Jul 3, 2020 at 17:14
  • Hi Muhammad, No it's not declared in rules, they should be? Commented Jul 3, 2020 at 17:21
  • 1
    yes it should be, otherwise it wont load the value Commented Jul 3, 2020 at 21:55
  • 1
    @MuhammadOmerAslam There is no load() call here, so rules() should not matter. Commented Jul 4, 2020 at 8:44
  • @rob006 you are damn right , i overlooked that there isnt and $model->load() call Commented Jul 6, 2020 at 4:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

They take always null value, with 2 methods, i don't know why??!!
@Belicoff Check that you have get parameter assigned and in the model->save() make 1-st line to check if you get desired parameter in the model.
this is my controller $isSuccessfullySaved = $leadModel->save(Yii::$app->request->get('keyword')); if (!$isSuccessfullySaved) { $response = $this->asJson(['errors' => ['webservice' => 'Error while saving the lead']]); $response->statusCode = 550; return $response->send(); }
My Model --> public function save($keyword) { $httpClient = new Client(); $data = [ 'operateur' => $this->operator, 'tel_domicile' => $this->phone, 'keyword' => $keyword, ]; }
But the link you have https://example.com/?key=test&id=1234 which mean you have to pass the \Yii::$app->request->get('key'), but you pass $leadModel->save(Yii::$app->request->get('keyword')); <-- which may not even exist.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.