Question

Question

I'm working with a database with composite primary key, and CakePHP doesn't support that.

So, when I save some information, CakePHP try to get the last inserted id, but return some errors (exactly because this table/model not have 'one primary key' but two or more.

So, how can I disable that?

Note: I want get the last query executed and all other informoations, just not the last inserted id action.

Tests

I try a lot solutions for modificate CakePHP support to compositve primary key, but, only the modifications on core works, and I know this is not a good pratice, so I discarded.

Modifications in CakePHP core for work with composite primary keys

Not a good pratice but, if you need

Note.: I try do my best but my code maybe is not the best option for do that, and sorry for the "multiplePrimarykey, and not use compositePrimaryKey".

git diff: https://gist.github.com/patrickmaciel/7662749

In summary

// Add this in your model
public $primaryKey = false;
public $multiplePrimaryKey = array('key_one', 'key_two');

// Done :)

Environment

  • CakePHP 2.4.3
  • Composer
  • PostgreSQL 8.4
  • Windows 7 x64
  • Sublime Text 2

Sorry for my english

Was it helpful?

Solution 2

I follow the instructions of @mark blog, and I can use my custom Model.php and DboSource.php without change the original files of CakePHP core.

Post: http://www.dereuromark.de/2011/10/31/freshly-baked-cake2-0-tips/

Using own classes instead of core ones

Let’s say, you want to apply a fix to a core file without overriding the core folder. Or you want to replace a file altogether.

Simply use the same folder structure inside the /Lib folder. For your own “FormAuthenticate” /app/Lib/Controller/Component/Auth/FormAuthenticate.php

Diff with CakePHP core files

The diff of my code and original cakephp core you found here: https://gist.github.com/patrickmaciel/7662749

Solution

I create a repository in github with all files needed for composite primary key work in CakePHP 2.x.

github: https://github.com/patrickmaciel/cakephp-composite-primary-key

This change allows you to use composite primary in your models, following this instructions:

// Add this in your model
public $primaryKey = false;
public $compositePrimaryKey = array('key_one', 'key_two', '........');

I hope this helps someone.

OTHER TIPS

Due the lack of support of composite keys in CakePHP you will have to perform your operations using Model::query()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top