0

足場で Cake PHP を使用しています。生成されるコードに問題があり、最終的にカスタム ビューを作成する必要があるかどうかを回避する方法があるかどうかを確認したいと考えています。

テストと質問の 2 つのモデルがあるとします。テストには多くの質問を含めることができますが、質問には 1 つのテストしかありません。hasMany および belongsTo アソシエーションをセットアップしました。

Cake がテスト用に作成する scaffolded ビューの [関連する質問] の下部に、質問を作成するためのボタンが表示されます。このボタンをクリックすると、質問の [追加] フォームが表示されますが、正しいテストが自動的に選択されません。

ボタンに test_id を Question フォームに渡させて、自動入力させることはできますか?

4

3 に答える 3

0

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:

// action: tests/view/1 (viewing test 1, and all related questions)
// create a link containing the ID of the current test as a param
<?php echo $this->Html->link('Add Question to Test', 
                                          array('controller'=>'questions',
                                                'action' => 'add_question',
                                                $test['Test']['id'])
                             );
?>

So - assuming you have access to id of the current test, you can pass it as a parameter to your questions controller (there are several ways to do this).

Then:

// view - questions/add_question/1
<h1>Adding A Question to Test 1</h1>
<?php 
// create your add question form

$this->Form->input('test_id', array('type'=>'hidden', 
                                  'value' => $this->params['pass'][0])); 
// create a hidden field with the value of the first passed param (the test id)

then in your controller, the test_id is already set, so when you save the question, it is saved with the appropriate test_id

于 2012-05-30T21:52:01.270 に答える
0

Cake Baker を使用して生成されたすべての CakePHP プロジェクトにこれを適用したい場合は、 https ://github.com/srs81/cakephp/commit/ に示すように、これを有効にするために CakePHP コアにいくつかの小さな変更を加えることができます。 7d92c8f676c79185fa6a74ab2070f240c555a2a0

基本的に、これら 2 つの変更により、参照モデル/コントローラーの ID と名前が「追加」アクションに追加されます。これは、正しい ID が選択された「追加」アクションで処理されます。

これは HABTM モデルでは機能しませんが、それ以外のモデルでは問題なく機能するはずです。

于 2012-05-30T22:21:43.133 に答える
-1

var $uses = array('Question','Test');question_controller.phpに追加する必要があります

于 2012-05-30T20:32:36.317 に答える