The Homepage of Daniel Hollands: Web Development Graduate!
9 Apr 2010
Back when I was building Unknown Tales as part of my Independent Study I had a problem trying to get data to save to three separate models at the same time. I was creating a form based around the Story model, and trying to get it to save data to that, as well as the Chapter and Path models at the same time.
Well, I’m happy to say that the latest edition of Unknown Tales, which is starting to come together very nicely, has fixed this problem, and it is such a simple fix that I’m kicking myself for not thinking about it before.
The models had the following relationship:
The issue, so far as I can recall, is that the data/validation was working correctly for the Story and Chapter models, but not the Path model. I think this was because the only way that Story had access to Path was via Chapter, and this appeared to be one step too far.
So how did I fix it? Simple – rather than basing the form around the Story model, I based it around the Chapter model.
As the chapter model has direct relationships with both Story and Path, it didn’t need to try and access any models via other models, it can do it directly.
All I had to do to fix the problem was allow the stories_controller.php file have access to the Story model via:
var $uses = array('Chapter', 'Story');
then change the line at the top of the form to:
$this->Form->create('Chapter', array('url' => array('controller' => 'stories', 'action' => 'add')));
and it now works as intended.
At the time this was causing me a major problem, and the only way I was able to resolve it was by creating two forms, one for the Story data, and a separate one for the Chapter and Path data. Now all three are handled by the same form, and all is well with the world.
I hope that this can help someone else with the same problem
One Response for "Fixed the three model problem"
[...] Daniel 28 Oct 2009Update – I’ve fixed itI’ve been able to fix this problem, check out my solution.This past week or so has not been my best. I’ve managed to use, and then break, two different [...]