# Save data to DB happened in weired way(Solved)

**URL:** <https://discourse.cakephp.org/t/save-data-to-db-happened-in-weired-way-solved/11077>\
**Category:** Need Help\
**Tags:** help\
**Created:** [March 21, 2023, 7:47am UTC](https://discourse.cakephp.org/t/save-data-to-db-happened-in-weired-way-solved/11077 "2023-03-21T07:47:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlc](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@rlc](https://discourse.cakephp.org/u/rlc)\
**Post date:** [March 21, 2023, 7:47am UTC](https://discourse.cakephp.org/t/save-data-to-db-happened-in-weired-way-solved/11077/1 "2023-03-21T07:47:48Z")

</div>

Greetings, I’m new to this community and Cakephp.  
I was trying to save data to my database(MySQL).

Here’s my db structure  
 ![螢幕擷取畫面 2023-03-21 153602](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/c0da99d95c6ef4ee2a598c3081139e19263def10.png)

and here’s my code

```auto
        $connection = ConnectionManager::get('default');
        $connection->begin();

        $indexData = [
            "customer_id" => 555,
            "zip_code" => 1,
            "address" => 2,
            "ship_fee" => 3,
            "discount" => 4,
        ];
        $entity = $this->OrderIndex->newEntity($indexData);
        $res = $this->OrderIndex->save($entity);
        $connection->commit();

```

but only “customer\_id” will be stored, but others will not.

---

<div class="post-metadata">

**Author:** ![rlc](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@rlc](https://discourse.cakephp.org/u/rlc)\
**Post date:** [March 21, 2023, 8:23am UTC](https://discourse.cakephp.org/t/save-data-to-db-happened-in-weired-way-solved/11077/2 "2023-03-21T08:23:43Z")

</div>

I figured it out myself.  
CakePHP has cache for models.  
Once I cleared it, it works just fine.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 21, 2023, 1:34pm UTC](https://discourse.cakephp.org/t/save-data-to-db-happened-in-weired-way-solved/11077/3 "2023-03-21T13:34:30Z")

</div>

Nice that you have found the solution yourself 👍

I just wanted to inform you, that CakePHP does transactions automatically when saving entities.

So you don’t need to manually do

```auto
$connection = ConnectionManager::get('default');
$connection->begin();
// Other code
$connection->commit();

```

but this

```auto
$indexData = [
    "customer_id" => 555,
    "zip_code" => 1,
    "address" => 2,
    "ship_fee" => 3,
    "discount" => 4,
];
$entity = $this->OrderIndex->newEntity($indexData);
$res = $this->OrderIndex->save($entity);

```

will automatically create a transaction

See [Saving Data - 4.x](https://book.cakephp.org/4/en/orm/saving-data.html#saving-entities)

> When saving, CakePHP will apply your rules, and wrap the save operation in a database transaction.
