私は、Course、Test、Question、Answerなどのモデルを持つeLearningサイトをCakePHPに構築しています。ここでCakePHPのマルチレベルモデルの関連付け
私の団体は、次のとおりです。
- コースhasManyのテスト
- テストbelongsToのコース
- 質問belongsToのテスト
- 質問は、このモデル
- 回答belongsToの質問
回答hasManyの協会はmoでうまくいくあなたの状況。たとえば、コースの焼きたてのCRUDアクションには、それに属するテストがあり、質問には回答が関連付けられていることが分かっています...
しかし、私はテスト管理ツールTestsControllerと関連レベル内で動作する質問はQuestionで終了します。答えはありません。言い換えれば、私が働いているビューは焼きTestsControllerからデフォルトでこれを持っている:あなたが見ることができるように
Array
(
[Test] => Array
(
[id] => 1
[name] => Test 1
[course_id] => 1
[time_limit] => 30
[max_questions] => 20
[randomize_order] => 1
[num_questions] => 2
)
[Course] => Array
(
[id] => 1
[course_identifier] => TLE1001
[title] => Child Development I
[description] =>
)
[Question] => Array
(
[0] => Array
(
[id] => 1
[text] => What is your name?
[test_id] => 1
[order] => 0
)
[1] => Array
(
[id] => 2
[text] => What is my name?
[test_id] => 1
[order] => 0
)
)
)
、私の見解では$のテストアレイは質問で停止し、関連を引っ張っていません質問の回答。
私はTestsControllerで私の「ビュー」アクションの下部に変更することで、この問題を修正:それは文字通り、手動で前に$テスト[「質問」]のすべての内容を置き換えるので、私は真ん中の行を追加
$test = $this->Test->read(null, $id);
$test['Question'] = $this->Test->Question->find('all'); //I added this line...
$this->set('test', $test);
をビューが使用する$ test配列を設定します。
Array
(
[Test] => Array
(
[id] => 1
[name] => Test 1
[course_id] => 1
[time_limit] => 30
[max_questions] => 20
[randomize_order] => 1
[num_questions] => 2
)
[Course] => Array
(
[id] => 1
[course_identifier] => TLE1001
[title] => Child Development I
[description] => The first years of life offer a window of opportunity for a child’s development. A solid understanding of how children grow and develop is essential to providing a quality care environment.
)
[Question] => Array
(
[0] => Array
(
[Question] => Array
(
[id] => 1
[text] => What is your name?
[test_id] => 1
[order] => 0
)
[Test] => Array
(
[id] => 1
[name] => Test 1
[course_id] => 1
[time_limit] => 30
[max_questions] => 20
[randomize_order] => 1
[num_questions] => 2
)
[Answer] => Array
(
[0] => Array
(
[id] => 1
[question_id] => 1
[text] => My name is what I say it is.
[correct] => 1
)
[1] => Array
(
[id] => 2
[question_id] => 1
[text] => My name is Earl.
[correct] =>
)
)
)
[1] => Array
(
[Question] => Array
(
[id] => 2
[text] => What is my name?
[test_id] => 1
[order] => 0
)
[Test] => Array
(
[id] => 1
[name] => Test 1
[course_id] => 1
[time_limit] => 30
[max_questions] => 20
[randomize_order] => 1
[num_questions] => 2
)
[Answer] => Array
(
[0] => Array
(
[id] => 3
[question_id] => 2
[text] => None of your business
[correct] => 1
)
[1] => Array
(
[id] => 4
[question_id] => 2
[text] => Jim
[correct] =>
)
)
)
)
)
私は達成するために持っているもののために動作しますが、それは私には醜いようだ:ここで
は結果です。あなたが見ることができるように、Answer(s)がロードされますが、質問モデルをロードするときに、関連付けられたTestで引っ張るので、私は現在情報を重複しています。そして、私はこれがCakePHPの慣行に気づかないと確信しています...
もっと良い方法がありますか? CakePHPは、あるレベルまでのモデルアソシエーションに関連するデータ配列のみを構築し、停止しますか?
P.S.私は追加しようとしました:
var $ uses = array( 'Test'、 'Question'、 'Answer');
...私のTestsControllerのトップにありますが、問題は解決されておらず、効果がないようです。
...だけでなく
このクエリでも私のために何も変更されません。結果$ testには依然として質問のみがあり、回答はありません。あなたが送ったリンクを見て、これも試しました: '$ test = $ this-> Test-> find( 'all'、array( 'contains' => array( 'Question' => array( 'Answer')) )); 'どちらも動作しません。私も '' contains '=> array(' Answer ') 'を入れてみましたが、うまくいきませんでした。 –
エラーは何ですか?また、それはあなたのCakePHPのバージョンに依存します - 以前のバージョンでは、Containableビヘイビアを明示的に追加する必要があるかもしれません。 – Suman