Assign custom value after consuming JSON API

Hi all.

I’m having a problem I cannot solve by myself.

I have a controller that gets values from two consecutive JSON Apis.

I get proper values from Api calls without problems, but when I’m trying to assign and save the model one field does not gets into the table, returning always a NULL.





    public function actionCreate()

    {

        $api_error = '';

        $model = new Clients();

        $model->create_time = date("Y-m-d H:i:s");




/**

* Goto API  y create advertiser

* brand->advertiser->create

* then create AdvertiserUser

* brand->AdvertiserUser->create

* then create register in DDBB

*/

        if (Yii::$app->request->post('status') !== NULL){ // if it's post


                // $advertiser = new HO_API_Brand(); 

                // /* create advertiser brand->advertiser->create */

                $target = 'Advertiser';

                $method = 'create';

                $args = array(

                	'company'=>Yii::$app->request->post('Clients')['company'],

                	'country'=>Yii::$app->request->post('Clients')['country'],

                	'phone'=>Yii::$app->request->post('Clients')['phone'],

                	'status'=>Yii::$app->request->post('Clients')['status'],

                	'zipcode'=>'55555'

                );


                	$resp = $advertiser->createAdvertiser($target, $method, $args);

echo "<pre>";

print_r($_POST);

print_r($resp);

echo "</pre>";    

            

                // if (is_array($resp) && array_key_exists('status',$resp) && $resp['response']['status'] != 1){

                if (!array_key_exists('Advertiser', $resp)) {

                    $api_error = $resp['response']['errors'];

                } else {

                    $model->advertiser_id = $resp['Advertiser']['id'];

                    // /* create AdvertiserUser brand->AdvertiserUser->create */           

                    $update_advertiser = new HO_API_Brand(); 

                    $target = 'AdvertiserUser';

                    $method = 'create';

                    $args = array(

                    	'advertiser_id' => $resp['Advertiser']['id'],

                    	'email'=>Yii::$app->request->post('Clients')['email'],

                    	'first_name'=>Yii::$app->request->post('Clients')['first_name'],

                    	'last_name'=>Yii::$app->request->post('Clients')['last_name'],

                    	'password'=>Yii::$app->request->post('Clients')['password'],

                    	'password_confirmation'=>Yii::$app->request->post('Clients')['password'],

                   );


                   $resp2 = $update_advertiser->createAdvertiserUser($target, $method, $args);


echo "<pre>";

print_r($resp2);

echo "</pre>";


                    if (!array_key_exists('AdvertiserUser', $resp2)) {

                        $api_error = $resp2['response']['errors'];

                    } else {

                    /* Ergo, if no errors */

/* NOW different approaches, even hard assign, not all at same time, obviously, just to mention */

                        // $model->advertiser_id = $resp2['AdvertiserUser']['advertiser_id'];

                        // $model->advertiser_id = $resp['Advertiser']['id'];

                        Yii::$app->request->post('Clients')['advertiser_id'] = $resp2['AdvertiserUser']['advertiser_id'];

                        $_POST['Clients']['advertiser_id'] = $resp2['AdvertiserUser']['advertiser_id'];

                        Yii::$app->request->post('Clients')['advertiser_id'] = '999';

                        $_POST['Clients']['advertiser_id'] = '888';

                    }

               } // end second IF API CALL AdvertiserUser->create


// Yii::$app->request->post('Clients')['advertiser_id'] = '999';

// $_POST['Clients']['advertiser_id'] = '888';               

// print_r("<hr> id 1 is...:".$model->advertiser_id);

 

        } // end if es POST


        if ($model->load(Yii::$app->request->post()) && $model->save() && !is_array($api_error)) {

echo "<pre>";

print_r(Yii::$app->request->post());

print_r($_POST);

echo "</pre>";

exit("<hr>id 3 is...:".$model->advertiser_id); // THIS IS THE EXPECTED VALUE, BUT ALWAYS NULL !!!!!*****

 

            Yii::$app->getSession()->setFlash('success', 'Your advertiser was created successfully!');

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model' => $model,

                'api_error' => $api_error

            ]);

        }

    }



Please, do excuse debugging mess.

[size=2]advertiser_id i[/size]t’s included as ‘safe’ in model validation





    public function rules()

    {

        return [

            [['create_time', 'company', 'first_name', 'last_name', 'country', 'email', 'phone', 'password', 'advertiser_id', 'status'], 'required'],

            [['create_time','advertiser_id'], 'safe'],

...



$model->advertiser_id should receive api value, and saved into table, but [size=2]it [/size][size=2]is [/size][size=2]always saved as null, even when I set the value manually.[/size]

[size=2]advertiser_id is integer.[/size]

Any comment or clue will be grateful.

Why is this line commented?


// $advertiser = new HO_API_Brand();

Does this work when the above is commented?


$resp = $advertiser->createAdvertiser($target, $method, $args);

Sorry. It’s just a typo. It shouldn’t be commented.

Not tested, but obviously it shouldn’t work. I have to instantiate the model first in order to work properly.

HO_API_Brand is the model handling the API call, and I instantitate it two times for consuming two different API calls, create advertiser and create advertiserUser.

Anyway, thanks for your time.

OK. Answering my own question, the problem lies on:




...

if ($model->load(Yii::$app->request->post()) && $model->save() && !is_array($api_error)) {

...




If I load the post array into the model, validate it, then save, it ‘protects’ the post array for being manipulated in any form, so I cannot assign a new value before [size=“2”]auto assignment, [/size][size=“2”]validation and saving.[/size]

So my solution was breaking it in parts, and assign the new value after validation:




.....


        if ($model->load(Yii::$app->request->post()) && !is_array($api_error)) {

// hera I assign the new value to the model

            $model->advertiser_id = $resp2['AdvertiserUser']['advertiser_id'];

// then save the model

            if($model->save()){

.....



This way I can assign the result of the API call to any value.

Not sure if it’s the right way to proceed, but… Any clue would be grateful.

Hope this helps anybody saving some hours…

Kind regards.