Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:12:57+00:00 2026-05-29T18:12:57+00:00

This is a fairly basic question about CakePHP, but since my knowledge of this

  • 0

This is a fairly basic question about CakePHP, but since my knowledge of this framework is rather rusty, it is making me lose a lot of time.

I have a ManyToMany relation between Guest and Present. Whenever a new Guest is created and associated with a present, I would like to mark the Present as taken. If the present is already taken, some error should arise. The reason why I am not just declaring that a Guest hasMany Presents is because in the future things may change and more than one guest could associate to a present, so I prefer to avoid a Db migration.

My Guest::add() action looks like follows. It is called with a POST with the data of a new Guest and the id of an existing Present.

public function add() {
    if ($this->request->is('post')) {
        $id = $this->request->data['Present']['id'];
        $this->Guest->create();
        $present = $this->Guest->Present->findById($id);
        if ($present['Present']['taken']) {
            throw new ForbiddenException();
        }

        if ($this->Guest->save($this->request->data)) {
            if ($this->Guest->Present->saveField('taken', true)) {
                 // Give the guest a uuid and proceed with a welcome message
                 $this->Guest->read();
                 $this->set('uuid', $this->Guest->data['Guest']['uuid']);
            }
        }

    }
    else {
        throw new ForbiddenException();
    }
}

What happens is that a new Guest is created (correct) and associated with the given present (correct) but when I save the taken field a new present is created instead of modifying the given one.

What is the correct way to proceed to update the current Present?

If it is of any help, I am using CakePHP 2.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:12:58+00:00Added an answer on May 29, 2026 at 6:12 pm

    For obtaining the model data by the primary key it’s better to use theIn addition read method:

        $present = $this->Guest->Present->read(null, $id);
    

    The read method sets the model’s id attribute so that further calls to other methods affect the same data record, rather than creating a new one. This should solve the problem you are having.

    Model callbacks tend to be better suited for these situations. You could add a beforeSave callback to the Guest class to checks if the present is already taken, and not allow the creation if it is. This way the model logic is left in the model layer and you don’t need to do any extra work e.g. if the constraint has to be enforced also when existing Guests are saved, or created from different controllers or actions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a fairly basic question about state server but assume there are 2
This is a fairly basic question, but I haven't been able to find any
All, This would seem like a fairly basic asp.net question - but in all
This is probably a fairly basic question but I haven't managed to find anything
This is a fairly basic question, which for some reason, a proper solution escapes
I know this is fairly subjective, but I'm diving into testing and learning about
first post, so play nice! I have a fairly basic question about Python dictionaries.
Ok, this is a fairly basic question: I am following the SICP videos, and
This may seem like a fairly basic question, for which I apologise in advance.
This is probably a fairly easy question, but I'm new to JavaScript and jquery....

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.