1

well I'm post data json to my backend and I have this error:

{
  "message": "Record not found in table \"users\" with primary key ['6']",
  "url": "/users/userdata",
  "code": 404
}

I don't know why? I have a user with id = 6 in my database, in my function I recieve the data and id correctly, check this part, my controller:

  public function userdata()
    {
        $user = TableRegistry::get('Users');
        $id = $this->request->getData('iduser');

        $data = $user->get($id, [
            //'contain' => ['Cities']
        ]);

        $this->set([
            'success' => true,
            'data' => $data,
            '_serialize' => ['success', 'data']
        ]);
    }

posting data

Try this with $this->Users->get($id) and $this->Users->find($id) I always get the same error

ndm
  • 59,784
  • 9
  • 71
  • 110
CoolLife
  • 1,419
  • 4
  • 17
  • 43
  • will you please debug `$id` before `get()` query, and check the value – tarikul05 Apr 19 '17 at 03:13
  • I already did this the value I get is 6 – CoolLife Apr 19 '17 at 03:18
  • what is type of this `$id` its `int` , `string` or `array` ? – tarikul05 Apr 19 '17 at 03:21
  • I thought about this before so I tried to convert to int the data I received a string ´$data = (int)$id;´ of this way I tried to convert to int and not work – CoolLife Apr 19 '17 at 03:34
  • `['6']` looks like array, that's why asking the actual type of `$id` – tarikul05 Apr 19 '17 at 03:55
  • I send this format of json { "iduser": 6 } and try with this too { "iduser": "6" } , [6] and ['6'] even if I transform to int this not work, – CoolLife Apr 19 '17 at 04:06
  • is_array($id); is false when I debug this – CoolLife Apr 19 '17 at 04:08
  • Post the javascript. You could $id = (int) $this->request->getData('iduser'); to make sure it isn't a type issue – Derek Apr 19 '17 at 04:19
  • I'm using postman for test. I did not javascript first created the backend after i do the frontend – CoolLife Apr 19 '17 at 04:22
  • Whenever receiving errors, please always post **the _complete_ error**, that is, **including the _full_ stacktrace** (ideally copied from the logs where it is available in a properly readable fashion), even if the problem might be obvious to people who are familiar with CakePHP! – ndm Apr 19 '17 at 10:38

1 Answers1

1

The error message can be a little bit misleading, as one could think that it's really about trying to access something in the database, however it's actually a InvalidPrimaryKeyException exception (whenever receiving errors, check the logs!).

This happens when the passed primary key format doesn't match the primary key configuration of the table, like for example when the primary key is a composite key, and you're not passing all required columns. It could also happen in case the tables primary key is wrongly configured, for example when it's empty.

Make sure that you're passing all required columns, check your tables setPrimaryKey()/primaryKey() call, make sure to clear the mode cache (tmp/cache/models) to avoid wrong/old schemas to be used, and ensure that the primary key constraint in the database is configured properly.

ndm
  • 59,784
  • 9
  • 71
  • 110