PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 14, 2022

[FIXED] Specifying saveStrategy for belongsToMany on save?

 January 14, 2022     cakephp-3.0     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing