Answer the question
In order to leave comments, you need to log in
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']
];
}
}
Answer the question
In order to leave comments, you need to log in
Parsers configured? Is this what you have?
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question