C
C
criminalist2019-12-07 06:46:59
RESTful API
criminalist, 2019-12-07 06:46:59

How to write data from JSON to YII2 RESTapi database?

Hello, not so long ago I started to master YII2 as an API.
In general, I created an API namespace, created models and controllers.
There were no problems with obtaining data, with deletion the same way, but when a POST request is made, an entry is created in the database in which only the ID is created automatically, all other fields are empty,
for example, via curl: https ://site.ru/v1/user/ -d '[{""name:"Jhon","code":"12341"}]
' did not see this, an example of a model

<?php
namespace api\modules\v1\models;
use \yii\db\ActiveRecord;
/**
 * User Model
 *
 */
class User extends ActiveRecord
{
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
    return 'user';
  }

    /**
     * @inheritdoc
     */
    public static function primaryKey()
    {
        return ['id'];
    }

    /**
     * Define rules for validation
     */
    public function rules()
    {
        return [
            [['id', 'name', 'code'], 'required']
        ];
    }
}

Why are fields from JSON not processed and not written to the database?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
C
coderisimo, 2019-12-07
@criminalist

Parsers configured? Is this what you have?

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question