CodeIgniter Forums
Multiple primary keys for protected $primaryKey - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Multiple primary keys for protected $primaryKey (/thread-74524.html)



Multiple primary keys for protected $primaryKey - bustersg - 10-05-2019

PHP Code:
protected $primaryKey 'priKey1'// normal for a table having 1 pri key 

I have a combination of 3 pri key
Then how should I declare it?

PHP Code:
protected $primaryKey 'priKey1, priKey2, priKey3'// don't think this is correct
protected $primaryKey = array('priKey1''priKey2''priKey3'); // ci4 online help document does not show it to be this way 



RE: Multiple primary keys for protected $primaryKey - dave friend - 10-05-2019

I believe that any given SQL table can have only one primary key though the key may consist of several columns. In the multi-column case it would still only have one name which would be the value assigned to $primaryKey.


RE: Multiple primary keys for protected $primaryKey - InsiteFX - 10-06-2019

dave friend is correct a table can only have one primary key.
But it can have multiple other keys for indexing etc;


RE: Multiple primary keys for protected $primaryKey - dave friend - 10-06-2019

It's a good question if you're asking how to set up Model::primaryKey to support a multi-column primary key. The answer is: you cannot. The value assigned to Model::primaryKey must be the name of a single column. However, the column used need not be the actual PRIMARY key for the table. The property can use any column, but that column should contain unique values and ideally be indexed.

You could probably extend the Model class to handle a composite primary key. That would require overriding five or six functions to achieve the same functionality.

One of the best features of CI4 is its use of namespace and autoloading which makes it much easier to extend any class.