0

I have a MySQL archive table with the following structure:

    `histories` 
      `uid` int(10) unsigned NOT NULL,
      `type` varchar(255) NOT NULL,
      `param` int(11) NOT NULL,
      `param2` varchar(255) NOT NULL,
      `time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
     ENGINE=ARCHIVE DEFAULT CHARSET=latin1;

I do not want to add an auto-incrementing `id' column. This table's engine is archive and it does exactly what I need it to, with this structure.

I wish to save like this:

$this->History->create();
$this->History->save(['uid' => uid, 'type' => 'deleted_entry', 'param' => $thing_id]);

But CakePHP forces an update, which I don't want and the ARCHIVE engine doesn't support. Cake seems to be looking for a primary key of uid, finding it and deciding that it should update and there seems to be no flag available to force an insert. I do not want to resort to $this->Model->query().

UPDATE: Set $primaryKey = null in AppModel. $this->create(); will then insert.

class History extends AppModel {
    public $primaryKey = null;
}

If you want to do an update after, simply:

$this->History->primaryKey = 'uid'; before the save()

Vael Victus
  • 3,966
  • 7
  • 34
  • 55

1 Answers1

1

You can tell Cake 2 that you have no primary key by setting the model's $primaryKey property to null:

$this->History->primaryKey = null;
$this->History->create();
$this->History->save(['uid' => uid, 'type' => 'deleted_entry', 'param' => $thing_id]);
jeremyharris
  • 7,884
  • 22
  • 31
  • That's a user ID, not an auto-increment ID. I need to specify the user ID because the log pertains to a user. – Vael Victus May 23 '18 at 14:57
  • I misunderstood the meaning of `uid` in the question, so I've updated the answer. – jeremyharris May 23 '18 at 16:56
  • So, neither of those methods worked, but you gave me an idea: to set the $primaryKey = null in the model. That worked! (with just create()) Thanks :} – Vael Victus May 24 '18 at 01:18
  • Great, I'm glad I at least got you in the right direction. I will update the answer with your information for future users :) – jeremyharris May 24 '18 at 18:55