私はこれがボロボロのトピックであることを知っていますが、私は完全な初心者であり、かなり長い間この問題を回避しようとしてきましたが、それを得ることができませんでした.
投稿とタグの2つのテーブルがあります
このシナリオで定義される関係は、多対多です。したがって、「tbl_tags_has_tbl_post」という名前のテーブルがあります。これは、リレーション間の割り当てを保存しています。
「投稿」モデルで「タグ」モデルのデータを取得し、投稿フォームからタグのデータを保存できました。ただし、「tbl_tags_has_tbl_post」テーブルに値を割り当てることができませんでした。
CAdvancedArRelationship 拡張機能とプラグインを使用してみましたが、まだ役に立ちません。
これは私のDB構造をグラフィカルに示しています:
これは、Post テーブルで定義した関係です。
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'tbl_post_id'),
'author' => array(self::BELONGS_TO, 'User', 'tbl_user_id'),
'tagsRelation' => array(self::MANY_MANY, 'Tags', 'tbl_tags_has_tbl_post(tbl_post_id, tbl_tags_id)'),
'commentCount' => array(self::STAT,'Comment','tbl_post_id'), // this shall count the number of comments present
);
}
これは、タグ モデルで定義した関係です。
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'posts' => array(self::MANY_MANY, 'Post', 'tbl_tags_has_tbl_post(tbl_tags_id, tbl_post_id)'),
);
}
そして、これは私の PostController クラスの actionCreate() メソッドです:
public function actionCreate()
{
$model=new Post;
$tags = new Tags; // This is for initializing the second model
$model->tbl_user_id = Yii::app()->user->id;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Post'],$_POST['Tags']))
{
$model->attributes=$_POST['Post'];
$tags->attributes = $_POST['Tags'];
$valid=$model->validate();
$valid=$tags->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$tags->save(false);
// ...redirect to another page
//
}
if($model->save()){
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'tags' =>$tags,
));
}
だから今、私ができるようにしたいのは、投稿がいくつかのタグで作成されるたびに、割り当てテーブルも自動的に埋められるべきです、例えば:
tbl_post_id tbl_tags_id 1 1 2 1 2 2
そんな感じ。
CAdvancedAr Extension も使用しようとしましたが、実行できませんでした。私は本当に無知です。多くのことを試しましたが、割り当てテーブルに値を挿入できませんでした。
拡張機能を使用する場合、このシナリオでその方法を説明してください。
どこが間違っているのか、何をする必要があるのか、誰か教えてもらえますか? この質問が本当に古いものである場合は申し訳ありません。しかし、私は本当にここで立ち往生しています。
よろしく、