0

I am trying to setup a simple Restfull api using cakephp. I followed the documentation from the Cakephp site. But I am encountering the following issue.

I am using Postman plugin to test the Api calls. I have a table called 'Categories' and in its controller have an action add().

public function add() {
    if ($this->request->is('post')) {
        $this->Category->create();
        if ($this->Category->save($this->data)) {
            $message = 'Saved';
        }
        else {
            $message = 'Error';
        }
        $this->set(array(
            'message' => $message,
            '_serialize' => array('message')
        ));
    }
}

and in db, I have Category table with schema (id (int 11, PK, A_I), name(varchar(40)), created (datetime), modified(datetime)).

So from postman I send POST requests to http://myProject/categories.json.

From my understanding when i send key:name and value: test, it must save into the database, which works fine. But I must get error when I replace the "key" with something other than name. i.e for exmaple key: name123 and value: test2, But this data too is getting saved in the db without any error except for the name field. i.e it is saving (id:some value, name:"", created:somevalue, modified:someValue) I dont understand how. Any help will be greatly appreciated.

3
  • From what you've described, I understood that key is an actual name of a column in your database table. And since you don't have the column name123, its normal for the error to be returned. Commented May 29, 2014 at 4:38
  • no. The fields are id, name, created, modified. By key, I meant to say the column name, i.e: id, name etc.... Commented May 29, 2014 at 4:48
  • Are you asking about validation ? that means with number it will not save anything.Is it the issue ? Commented May 29, 2014 at 12:49

1 Answer 1

0

You will need to provide more info because what you say doesn't make sense. For example what do your posted data look like? Is there any kind of validation in the model? How do you expect a key/value pair to be stored in only one field (specifically name) in the DB?

What happens now is that you tell Cake to save the supplied data ($this->Category->save($this->data)) although you don't check (via cake's validation rules in the model or any other means) that it contains useful arguments and especially Category.name.

As computers will just do what you tell them to do and not what you imply or have in mind, it goes on and sends the calculated created/modified fields to the DB which in turn saves them with the autoincremented ID. Since name doesn't have a UNIQUE or NOT NULL field condition in the DB it is saved as NULL or empty string.

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

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.