Associations not saving?

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:

$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:

  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

        // do some checks here...

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


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

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.

what about accessible ?

It’s in there.

in ProjectsDescriptions since you have problem with it

There’s nothing really in there :\

try to add id => true, there

Added it, tried saving again but nothing :\

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

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

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:

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…

ah try setting projects_descriptions in $project as dirty

That worked :slight_smile:
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…

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

1 Like

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

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.:grinning:

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.

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.