私は、多対多の関係を経験していないことが原因であると信じていることに行き詰まりました。
私のアプリケーションでは、関心を多対多の製品にマッピングしています。チェックボックス付きの特定の製品の下に、関心のある完全なリストが表示される次のシナリオを作成したいと考えています。フォームの送信時に選択されているチェック ボックスごとに、InterestProductAssignment テーブルに行が追加されます。
私の製品コントローラーでは、興味の完全なリストを呼び出します-
$interests = Interest::model()->findAll();
実際、ここからどこに進むべきか頭が混乱しているので、これ以上先に進むことはできません。これまでに試したことは、上記で返された興味配列と一致するように InterestProductAssignment オブジェクトの配列を作成することです。それをビューに渡してフォームを構築しようとしましたが、2 つを一致させようとしてかなり混乱してしまい、Yii を正しく使用しているとは信じられません。
送信時に製品と興味の間のリンクを追加するすべての興味に対してチェックボックスを使用できるようにする、この問題の解決策の概要を説明できる人はいますか? コントローラーとビューに何を書くかを知りたいです。
明確にするために、このページは 1 つの製品のみを対象としています。
拡大
私が抱えている補足的な問題については、私が持っているコードを投稿しています。これは、間違いを見つけたら、他の人が同様のことを実装するのにも役立つかもしれません.
製品コントローラー内の私の関係は次のようになります-
'interests'=>array(self::MANY_MANY, 'Interest', 'interest_product_assignment(interest_id, product_id)')
コントローラ
public function actionUpdate($id)
{
// loadModel as been adapted to be called "->with('interests')"
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Product']))
{
$model->attributes=$_POST['Product'];
if($model->save())
foreach($_POST['ProductInterest'] as $i => $interest_id){
$this_interest = new InterestProductAssignment;
$this_interest->product_id = $model->id;
$this_interest->interest_id = $interest_id;
$this_interest->save();
}
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
view _form.php の関連部分
<div class="row">
<?php echo CHtml::checkBoxList('ProductInterest', CHtml::listData($model->interests, 'interest_id', true), CHtml::listData(Interest::model()->findAll(), 'id', 'interest'));?>
</div>
問題は、checkBoxList の 2 番目のフィールドが、既に選択されているチェック ボックスに適切に入力されていないように見えることです。これの根本的な原因はおそらくばかげた間違いだと思います。私はそれを見つけることはできませんし、checkBoxList についてもよく知りません。
前もって感謝します。