# \[solved\] Saving entity with "custom" id

**URL:** <https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073>\
**Category:** Need Help\
**Created:** [May 13, 2019, 9:17am UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073 "2019-05-13T09:17:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [May 13, 2019, 9:17am UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/1 "2019-05-13T09:17:55Z")

</div>

Hii there,

I’m trying to import users and data into my app.  
The data for a user is build and saved like this:

```php
$data = [
        'id' => $user['ID'],
        'username' => $user['user_nicename'],
        'email' => $user['user_email'],
        'password' => $user['user_pass'],
        'created' => $user['user_registered'],
        'users_detail' => [
          'firstname' => $user['user_nicename'],
          'lastname' => ''
        ],
        'roles' => [
          ['id' => $memberRole->id]
        ]
];
$usersEntity = $usersTable->newEntity($data);
$usersTable->save($usersEntity);

```

The user is inserted just fine except with one issue: the `id` is just plainly ignored and keeps following the `autoincrement` from the database.  
Is there a way to insert the user with the proper ID?  
Just rolling with it like this will complicate matters a lot as now the data from the other tables I need to migrate won’t align with the users at all (eg. the user with `id=3` in the database actually is known with `id=9`, the one with `id=4` in the database actually is known with `id=29` etc.)

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [May 13, 2019, 9:30am UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/2 "2019-05-13T09:30:38Z")

</div>

adding `$usersEntity->id = $user['ID'];` before saving it does the trick

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 13, 2019, 2:14pm UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/3 "2019-05-13T14:14:40Z")

</div>

The default code baked for you will exclude the primary key from being assigned in a patch call. Look at the `_accessible` property in your entity. There’s good security reasons to leave it like that most of the time; see [Bypassing Field Guarding](https://book.cakephp.org/3.0/en/orm/entities.html#bypassing-field-guarding) for a solution that might work well for your use case.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [May 13, 2019, 2:57pm UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/4 "2019-05-13T14:57:30Z")

</div>

> [@Zuluru](#):
>
> see [Bypassing Field Guarding](https://book.cakephp.org/3.0/en/orm/entities.html#bypassing-field-guarding) for a solution that might work well for your use case.

This might be a cleaner way of doing it yes 🙂  
I’ll test it out when I’m back at the office tomorrow.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [May 14, 2019, 7:48am UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/5 "2019-05-14T07:48:28Z")

</div>

Seems to work fine as well 🙂

---

<div class="post-metadata">

**Author:** ![deanoj](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@deanoj](https://discourse.cakephp.org/u/deanoj)\
**Post date:** [May 16, 2019, 12:40pm UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/6 "2019-05-16T12:40:12Z")

</div>

A word of warning if you allow the ID column to be mass assigned… If you have an action: ‘add’ controller/view that has the ID as a text input, and you submit the form with the same ID as an existing record in your table - that record will be _overwritten_ without warning.  
The approach I use now is the same as your first answer…

$entity-\>id = $data[‘id’];

However, I run a select to check for an existing record with the same ID forst in my controller - which allows my to return a validation error.

Just for context, my use case was dealing with Products (like ecommerce), and the Product SKU is a natural key usually, so I wanted to SKU to be the entity ID.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 16, 2019, 2:17pm UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/7 "2019-05-16T14:17:43Z")

</div>

This is why you’d want to leave the `_accessible` property set as it is by default, and use the one-time bypass method from my link only in specific cases like the OP has, where the data is all built from a trusted source.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [May 16, 2019, 3:36pm UTC](https://discourse.cakephp.org/t/solved-saving-entity-with-custom-id/6073/8 "2019-05-16T15:36:00Z")

</div>

Yea, it’s only a “one-time” thing because I had to import data (like users, blogposts, comments etc.) from my live website into my CakePHP app 🙂
