Using integer IDs


I’m discovering the back-end development in Shopware 6 by creating a plugin. My plugin connects with a API. I’m trying to use integers as foreign keys for my custom entity as the API deliver these. When I call my association I’m getting errors on the field that it isn’t binary. Anyone that can explain me how I can use integers as FK?

protected function defineFields(): FieldCollection{
            return new FieldCollection([
                (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
                (new IntField('parent_id', 'parentId')),
                (new IntField('original_id', 'originalId'))->addFlags(),
                (new JsonField('data', 'data'))->addFlags(new Required()),
                (new FkField('parent_id', 'parentId', self::class)),

                (new OneToManyAssociationField('children', CategoryDefinition::class, 'parent_id', 'id')),

IDs must always be UUID. So you cannot set it to the type IntField.

Why do you need to use it as a FK?

I’m using a store API from another platform. The categories list returns a ID (int) and parent_id (int|null). To build the category structure (as category → sub → sub sub), I need to be able to get the children.

To be honest, I can’t imagine that Shopware 6 doesn’t allow you to use int fields as ID. More platforms are using integers as IDs.

Hi Donny!

Shopware decided to use UUIDs and you can’t easily change that. But that is not needed. You can add two more fields for your ID and PARENT_ID.

Or if you want it a little more complex (but no need for two new fields) you can think about a mapping function which does int → uuid to do this technically you „only“ need to choose a nice prefix like

‚715221f8-da25-48ff-aa5b-‘ and then add 12 digits to it - tada you have a UUID :slight_smile: In php you can do this, e.g. by using str_pad($id, 12, 0, STR_PAD_LEFT)

Hope that helps, welcome to Shopware 6 and good luck! And I can recommend the Shopware slack:

Thank you for your answer. I was already thinking about mapping this data. Basically its a migration tool from one platform to another to experiment with Shopware. I also thought about prefixing the parent_id with a UUID. I think that thats my best option.

Thank you for your fast answer!

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.