CodeIgniter Forums
Using Update() on primary key named differently than id - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Using Update() on primary key named differently than id (/showthread.php?tid=79253)



Using Update() on primary key named differently than id - tomsenner - 05-18-2021

Hello,

when I am using the below code it will only work if my primary key field in the databaseĀ is named "id".
PHP Code:
$model->update($idVar, ['description' => $this->request->getPost('description')]); 

But because I will have to run many SQL Joins between tables I would like to be able to name my primary key fields differently.

How can I achieve this?

Thanks in advance for your help.


RE: Using Update() on primary key named differently than id - craig - 05-18-2021

Have you tried using the documentation's suggestion of setting the primaryKey variable?

https://codeigniter.com/user_guide/models/model.html#configuring-your-model

Code:
protected $primaryKey = 'other_id_field';



RE: Using Update() on primary key named differently than id - tomsenner - 05-18-2021

Thank you Craig. I was looking deeply into the documentation but must have missed this one.
It's excactly what I needed!