# Associations not saving?

**URL:** <https://discourse.cakephp.org/t/associations-not-saving/6534>\
**Category:** Need Help\
**Created:** [August 26, 2019, 2:23pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534 "2019-08-26T14:23:44Z")\
**Posts on this page:** 17\
**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:** [August 26, 2019, 2:23pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/1 "2019-08-26T14:23:44Z")

</div>

Hii there,

I’m trying to fix a bug where associations are not saving.  
In the regular controller, I’m using this piece of code to create the entity and an associated entity:

```php
$entity = $this->Projects->newEntity([
            'name' => $this->request->getData('name'),
            'source' => '#',
            'thumbnail' => '#',
            'license' => '',
            'projects_descriptions' => [
              ['description' => $this->request->getData('description')]
            ]
]);

```

If I save that, it works fine but now in another class (for my GraphQL API), I use the following code:

```auto
  class ProjectDataSource {
    private static $_projectsTable;
    private static $_projectsReleasesTable;

    public function __construct() {
      self::$_projectsTable = TableRegistry::get('Projects');
      self::$_projectsReleasesTable = TableRegistry::get('ProjectsReleases'); 
    }

    public function updateProject($id, array $data = []) {
        // Get the project by the requested it
        $project = self::$_projectsTable
          ->findById($id)
          ->contain([
            'ProjectsDescriptions'
          ])
          ->first();

        // do some checks here...

        // Update the entity
        $project->projects_descriptions[0]->description = $data['description'];

        self::$_projectsTable->save($project)
    }
}

```

Now the problem is that my `$project` object looks like this (take note of the `description` object being marked as dirty!):

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/aea0150cbace8a26f82257f966d4746860eb2815.png)

But saving it doesn’t change it in the database, and the entity is still marked as dirty.

When I also change the `$project->name` and then save, the name updates but the description still doesn’t.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 2:31pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/2 "2019-08-26T14:31:04Z")

</div>

what about `accessible` ?

---

<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:** [August 26, 2019, 2:36pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/3 "2019-08-26T14:36:57Z")

</div>

It’s in there.

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/5f492283637828430d98951d7595bf8d570cee86.png)

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 2:47pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/4 "2019-08-26T14:47:52Z")

</div>

in `ProjectsDescriptions` since you have problem with it

---

<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:** [August 26, 2019, 2:51pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/5 "2019-08-26T14:51:58Z")

</div>

There’s nothing really in there :\

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/e/ed09e5317c70ae70302d5e80d306bc5d498a0e65.png)

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 2:52pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/6 "2019-08-26T14:52:29Z")

</div>

try to add `id => true,` there

---

<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:** [August 26, 2019, 2:54pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/7 "2019-08-26T14:54:05Z")

</div>

Added it, tried saving again but nothing :\

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/3ea58127a32faffd50543678135ff83e04e7889b.png)

---

<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:** [August 26, 2019, 2:58pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/8 "2019-08-26T14:58:34Z")

</div>

Trying to use the “regular” way in the normal controller yields the same issue :\

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 3:14pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/9 "2019-08-26T15:14:13Z")

</div>

hmm in app.php enable query log check generated query, inspect it and if it looks ok copy it into your manager and check if there are errors

---

<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:** [August 26, 2019, 3:25pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/10 "2019-08-26T15:25:41Z")

</div>

I enabled the log and searched for the query… but there is no query that has to do with updating the entity…  
I see some `SELECT` statements that I expect to be there but no `UPDATE` statements…  
When I change the `$project->name`, I get the following query:

```auto
2019-08-26 15:24:43 Debug: duration=0 rows=0 BEGIN
2019-08-26 15:24:43 Debug: duration=13 rows=1 UPDATE projects SET name = '9AnimeDl1' WHERE id = 1
2019-08-26 15:24:43 Debug: duration=0 rows=0 COMMIT

```

As you can see, it doesn’t update the `description` even though it’s dirty…

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 3:31pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/11 "2019-08-26T15:31:20Z")

</div>

ah try setting `projects_descriptions` in `$project` as dirty

---

<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:** [August 26, 2019, 3:38pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/12 "2019-08-26T15:38:12Z")

</div>

That worked 🙂  
Do you by any chance have an idea on why this doesn’t go automatically?  
I expected to automatically mark it after I changed the entity…

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [August 26, 2019, 3:41pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/13 "2019-08-26T15:41:21Z")

</div>

i dont think it ever worked automatically thats why we have `$entity->set` and `$table->patchEntity`

---

<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:** [August 26, 2019, 3:46pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/14 "2019-08-26T15:46:51Z")

</div>

Well… this is awkward… I completely forgot the existence of `patchEntity()`…

---

<div class="post-metadata">

**Author:** ![spadeX](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@spadeX](https://discourse.cakephp.org/u/spadeX)\
**Post date:** [August 27, 2019, 5:01am UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/15 "2019-08-27T05:01:56Z")

</div>

Well that same issue occurred with my project. I would find out the issue. Actual problem is if the any of the database column has error or different datatype then data doesn’t not save by entity. if you check each field datatype and database table then you will get. if you don’t mind you can check.😀

---

<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:** [August 27, 2019, 6:12am UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/16 "2019-08-27T06:12:45Z")

</div>

Well… considering the datatypes for the entity stay the same, no error is being returned and the query just doesn’t even try to update the column in the first place, we could rule out a wrong data type.

---

<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:** [August 27, 2019, 3:40pm UTC](https://discourse.cakephp.org/t/associations-not-saving/6534/17 "2019-08-27T15:40:29Z")

</div>

You’re updating a `project_description` object directly, the `$project` variable is merely something that you traverse in read-only fashion to get to it. So there’s no way that `$project->dirty` will be updated by your change, and hence when you save `$project` it doesn’t think there’s anything to be done.
