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

Thursday, April 21, 2022

[FIXED] What is the best way to Update/Add/Delete multiple records in the same form with CakePHP

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

I have this Topic table who is linked to the table Post with hasMany in the model.

This is my $this->request->data

Array
(
[Topic] => Array
    (
        [id] => 1
        [topic_title] => This is my topic
    )

[Post] => Array
    (
        [1] => Array
            (
                [id] => 1
                [title] => Blah
                [message] => My message
            )

        [2] => Array
            (
                [id] => 2
                [title] => Second Blah
                [message] => Second My message
            )

    )

)

And I update it with :

$this->Topic->saveAssociated($this->request->data);

But What if I also wanted to have the ability to add and delete posts to this topic, how would I proceed ?


Solution

Keeping the same structure, you could do a $this->Topic->saveAssociated($this->request->data); and it will add any new ('id' => NULL or unset) items in the data array.

About the delete, the only case I know would delete at the same time, would be a HABTM when it's marked as 'unique' => true. Otherwise, you need to do a $this->Post->deleteAll(array('Post.topic_id' => $unwanted_topic_id), false);

I could think of making a new array keeping the ones you want deleted and sending them as condition for the deleteAll function.



Answered By - SrQ
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • 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