fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
802 stars 335 forks source link

composite primary keys in CRUD #2198

Closed polmartinez closed 8 months ago

polmartinez commented 8 months ago

how can we indicate that we use a PK of more than 1 field in crud models?

WanWizard commented 8 months ago

Afaik that is not supported.

matt-t-d commented 8 months ago

I think it can be done by adding it as the 3rd argument of \DBUtil::create_table e.g.

\DBUtil::create_table('posts', [
    'id' => array('type' => 'int', 'constraint' => 5),
    'title' => array('type' => 'varchar', 'constraint' => 100),
    'body' => array('type' => 'text'),
], ['id', 'title']);

Would make id and title the primary key.

The base model class itself has protected static $_primary_key = array('id'); so I imagine that just replacing the array with the multiple keys should be ok.

The docs (https://fuelphp.com/docs/packages/orm/crud.html) say you will have to pass an array to the find method e.g.

// ...or when using compound primary keys
$entry = Model_Article::find(array(2, 'foo'));
WanWizard commented 8 months ago

Aren't you mixing ORM models and Model_Crud ?

The ORM fully supports compound keys, but in Model_Crud, only a string is supported.

https://github.com/fuel/core/blob/1.9/develop/classes/model/crud.php#L22

matt-t-d commented 8 months ago

Yep, sorry. I assumed it was extending the Orm\Model class.

WanWizard commented 8 months ago

Ha, no, Model_Crud is an entry level basic model, only working only on a single table, using DB calls.