1
votes

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??

2

2 Answers

1
votes

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
votes

I'm assuming that you are using belongTo relationship, but I think it could be on any of the types described on the cookbook

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

Yes, this is handled automatically by Cake if you set the proper relationship (belongsTo, hasA, etc.)

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?

I believe this is handled with the saveAll, saveAssociated, etc. methods, but I haven't faced an scenario to use this yet

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??

This is because you're saving the main Model and it's dependencies (again it depends on how you establish the relationship), so you need to explicit say so. Other way to do this will be importing the StudenController (according to your example) and (after constructing the classes) call the save method $studentController->Student->save($student)

Hope this helps