I am using Cake PHP with scaffolding. I'm having a problem with the code that it generates and want to see if there is a way around it of if I should end up building custom views.
Lets say that I have two models Tests and Questions. Tests can have many Questions and a Question has only one test. I have setup the hasMany and belongsTo Associations.
Now, the scaffolded view that cake creates for Tests gives me a button at the bottom in the "Related Questions" to create a question. When I click this button, I get the 'Add' form for questions, but the right test is not auto selected.
Is there anyway I can make the button pass the test_id into the Question form and have that auto populate?
I see how you think that might work; but Cake doesn't know you want that behaviour out of the box.
You would need to adjust your
Question Add
method, or create a new one:Example code:
So - assuming you have access to
id
of the currenttest
, you can pass it as a parameter to yourquestions
controller (there are several ways to do this).Then:
then in your controller, the
test_id
is already set, so when you save the question, it is saved with the appropriatetest_id