0

I used yii2 restful API explained hear: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

and in CURL it works correctly for GET and DELETE. but when i use this in CURL:

curl -i -H "Accept:application/json" -H "Content-Type:application/json" 
-XPOST "my localhost path/customers"
-d '{"name": "xxxx", "family": "xxxx","mobile":"xxxxx","home":"xxxxx","work":"xxxx"}'

It inserts only auto increment field "id", in my table 'customer' and other fields in my database is empty!

2 Answers 2

1

I don't think this is related to the API functionality but fairly sure this is a "safe attribute" problem in your model.

Please take a look at this piece of documentation to make sure that you have marked all those other attributes as safe. Even via a REST call this is still a mass assignment, subject to the same rules.

Basically you either add them in your validation rules as safe (at the minimum) or have them returned as part of the safeAttributes()-result.

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

Comments

0

Thanks. I add the following code in my model class "Customer" and it works:

public function rules()
{
    return [
    [['name', 'family', 'mobile', 'home', 'work'], 'required'],
    [['name', 'family'], 'string', 'max' => 20],
    [['mobile', 'home', 'work'], 'string', 'max' => 11]
    ];
}

Comments

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.