Closed andrey-denisenko closed 4 years ago
What do you mean by "model with two primary keys"? There could be only a single primary key. If you're using multiple columns, that's compound primary key i.e. both id and timestamp are part of the key so "id" by itself is not primary key in this case.
What do you mean by "model with two primary keys"?
CREATE TABLE `MyModel` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`timestamp_utc` int(10) unsigned NOT NULL,
`type_id` int(11) NOT NULL,
`details` blob,
PRIMARY KEY (`id`,`timestamp_utc`),
)
So yes, right term will be "compound PK".
As I guess, mentioned extension shall use ActiveRecord::primaryKey() + in_array(), for example.
Then it works as expected. id
is not primary key. timestamp_utc
is not primary key. id
,timestamp_utc
is primary key.
The problem:
I have ActiveRecord model with two primary keys (id and timestamp). So when I doing check for primary key: $model::isPrimaryKey('id') it returns me FALSE.
Because body of isPrimaryKey is following:
So it does comparison amount of provided keys with amount of existing PK's. (what for???) This seems to be wrong, because 'id' is a primary key of my model, and obvious result shall be TRUE.
How it all happened?
I started to play with la-haute-societe/yii2-save-relations-behavior whose usage was proposed in this >>>article<<< This "helper behavior" does automation of saving of all related records.
So my model looks similar to this:
And when I trying to save my model with relations, this helper behavior is failing on call $model::isPrimaryKey() Because MyModel has two primary keys, when SaveRelationsBehavior provides into isPrimaryKey() only 'id' (which was extracted from information about this relation).
Peace of failing code from SaveRelationsBehavior:
So my question is:
It's the YII code is wrong? Or it's author of this SaveRelationsBehavior uses function isPrimaryKey() wrong way?