Friday, January 14, 2022

[FIXED] Specifying saveStrategy for belongsToMany on save?

Issue

I am using the default "replace" strategy in a belongsToMany relationship in my database. However, in one GUI situation, I need to use the "append" strategy instead.

How can I specify the saveStrategy for a belongsToMany relationship for a single save() call?

To be more specific, I can't use the link() method since my join table has other fields so I need to specify _joinData. Here's the code:

    $this->JobOrdersEducations->patchEntity($joe,
            [
                'degree_disciplines' => [
                    [
                        'id' => $degree_discipline_id,
                        '_joinData' => [
                            "criticality_id" => $criticality_id
                        ]
                    ],
                ]
            ]
    );
    $this->JobOrdersEducations->save($joe);

Solution

You can change the strategy on the fly:

$this->JobOrdersEducations->association('DegreeDisciplines')->saveStrategy('replace');


Answered By - Greg Schmidt

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.