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 8119285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:45:35+00:00 2026-06-06T04:45:35+00:00

after reading the cookbook and various Q&A here I am still a bit confused

  • 0

after reading the cookbook and various Q&A here I am still a bit confused conceptually about the model associations and what they do. And I was hoping that somebody can help me straighten this out. So here goes my question(s):

As far as I can understand model associations helps reading / finding associated data. So if I load a User then all Posts for that User will be loaded as well? But is it true that CakePHP doesn’t do much for saving new entries? For example: If I create a new Post then I have to specify the user_id field for the relevant User manually, CakePHP doesn’t do it automatically somehow? With manually I mean defining the mentor_id explicitly in this answer: CakePHP model associations table w/data and new table

And why is it necessary to “mention” both models when saving? $this->Mentor->Student->save($student) in example. Is it a way to keep track of two-way associations??

  • 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-06-06T04:45:37+00:00Added an answer on June 6, 2026 at 4:45 am

    If I load a User then all Posts for that User will be loaded as well?

    Yes, CakePHP will load all the associated data unless you unbind or bind the models or change the recursive property on the model you’re using.

    But is it true that CakePHP doesn’t do much for saving new entries?For
    example: If I create a new Post then I have to specify the user_id
    field for the relevant User manually, CakePHP doesn’t do it
    automatically somehow?

    You have to specify the ID for the user and then do save, when you save from the model with the specified ID it will handle the rest. For instance let’s say you have a Post and a User model, and the user is logged in using Auth:

      $this->request->data['Post']['user_id'] = $this->Auth->user('id');
      if ($this->Post->save($this->request->data)) {
        $this->Session->setFlash('Your post has been saved.');
        $this->redirect(array('action' => 'index'));
      }
    

    So it’s not really neccessary to mention both models, in the case that you mention both you can do the following, again with Posts and Users as example.

    $this->User->read(null, $this->Auth->user('id'));
    if ($this->User->Post->save($this->request->data)) {
      $this->Session->setFlash('Your post has been saved.');
      $this->redirect(array('action' => 'index'));
    }
    

    This would be equivalent to the previous case, since you are loading the User model with the user data and saving a Post for that User.

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

Sidebar

Related Questions

After reading RailsGuides , some answers here and other docs I am still confused
After reading similar questions with many variants, I'm still stuck on this issue. Here's
After reading a lot of on the subject I'm still in the dark about
After reading through various tutorials [1] I still can't get my VirtualHost settings to
After reading The JSR-133 Cookbook for Compiler Writers about the implementation of volatile, especially
After reading 100's of articles on here about how to create a DropDown List
After reading a bit more about how Gnutella and other P2P networks function, I
After reading tons of documentation and questions from here I'm still stuck with the
After reading MSDN-XAML Namespaces and MSDN-Understanding XAML Namespaces , I still do not understand
After reading the topic Is MD5 really that bad , I was thinking about

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.