cakephp3のアソシエーションの機能がいまいちよくわかっていないので、ご教示お願いします
TestsとProducutsという2つのテーブルがあります。
productsには製品情報、testsのほうにはその製品情報に紐づいたスペックを入れようと考えています。
productsとtestsは1対多の関係になると思うので、それぞれのtable.phpには下記のように記述しました。
php
1class ProdutsTable extends Table { 2 public function initialize(array $config) { 3 $this->hasMany('Tests'); 4 } 5} 6 7class TestsTable extends Table { 8 public function initialize(array $config){ 9 $this->belongsTo('Products'); 10 } 11}
controller側です
php
1public function index(){ 2 $tests=TableRegistry::get('tests');//アソシエーションテスト 3 $products=TableRegistry::get('products');//製品 4 5 $test=$products->find(); 6 $test->where(['p_id'=>'01hpe3']); 7 $test->contain(['Products','Tests']); 8 $test->order(['id'=>ASC]); 9 foreach($test as $test_values){ 10 debug($test_values); 11 } 12 }//index
テーブルの構造です。
products id | p_id | name 1 | 01hpe3 | お茶 2 | 3rsefg | コーヒー tests id | products_id | spec 1 | 1 | テアニン 2 | 1 | ビタミンC 3 | 1 | カフェイン 4 | 2 | カフェイン 5 | 2 | コーヒー豆
productsのp_idが01hpe3のidと一致するtestsのレコードを呼び出したいのですが、products is not associated with products というエラーが出てしまいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/11 04:07
2017/07/11 04:10
2017/07/11 04:58
2017/07/11 05:18 編集
2017/07/11 05:45