douxin8383 2016-06-28 08:31
浏览 61
已采纳

无法在Yii中的连接表中插入记录

I'm new to Yii2. I've three tables project, amenity and a junction table project_amenity, and here is the code:

Project Model:

<?php
namespace app\models;
use yii\db\ActiveRecord;

class Project extends \yii\db\ActiveRecord
{
    public function getAmenities()
    {
        return $this->hasMany(Amenity::className(), ['id' => 'amenity_id'])->viaTable('project_amenity', ['project_id' => 'id']);
    }

    public function rules()
    {
        return [
            [['name', 'city'], 'required'],
        ];
    }
}

Amenity Model:

<?php
namespace app\models;
use yii\db\ActiveRecord;

class Amenity extends \yii\db\ActiveRecord
{
    public function getProjects()
    {
        return $this->hasMany(Project::className(), ['id' => 'project_id'])->viaTable('project_amenity', ['amenity_id' => 'id']);
    }
}

ProjectController:

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\Project;
use app\models\Amenity;
use yii\helpers\ArrayHelper;
class ProjectController extends Controller
{
    public function actionCreate()
    {
        $project = new Project;
        $amenities = ArrayHelper::map(Amenity::find()->all(), 'id', 'name');

        if ($project->load(Yii::$app->request->post()) && $project->save()) {
            return $this->render(['confirm', 'id' => $project->id]);
        } else {
            return $this->render('create', [
                'project' => $project, 'amenities' => $amenities
            ]);
        }
    }
}

And here is the Incomplete create view:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;

?>
<?php $form = ActiveForm::begin(); ?>
    <table class="table">
        <tr>
            <td><?= $form->field($project, 'name'); ?></td>
        </tr>
        <tr>
            <td ><?= $form->field($project, 'city'); ?></td>
        </tr>
        <tr>
            <td><?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?></td>
        </tr>
    </table>

<?php ActiveForm::end(); ?>

ProjectAmenity Junction Model

<?php
namespace app\models;
use yii\db\ActiveRecord;

class ProjectAmenity extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'project_amenity';
    }
    public function rules()
    {
        return [
            [['project_id', 'amenity_id'], 'required']
        ];
    }
}
?>

I'am able to display the relational data but not able to insert. Please suggest how to display the amenity checkboxes on create view and and how to insert the data in project and project_amenity table from the create view.

  • 写回答

2条回答 默认 最新

  • duanba4942 2016-06-28 10:02
    关注

    Add amenities property to your project model:

    public $amenities;
    

    Add a checkboxlist to your view:

    <?= $form->field($project, 'amenities')->checkboxList($amenities) ?>
    

    Create junction model if not already exists.

    Loop through input in create action after save:

    if ($project->load(Yii::$app->request->post()) && $project->save()) {
        // checkboxlist fills amenities property with corresponding keys
        foreach($project->amenities as $amenity_key) {
            $project_amenity = new ProjectAmenity([
                'project_id' => $project->id,
                'amenity_id' => $amenity_key
            ]);
            $project_amenity->save();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办