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

时间:2016-06-28 08:31:38

标签: php mysql yii2

我是Yii2的新手。我有三个表项目,便利设施和一个联结表项目_amenity,这里是代码:

项目模型:

<?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'],
        ];
    }
}

舒适型号:

<?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
            ]);
        }
    }
}

这是不完整的创建视图:

<?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 交汇点模型

<?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']
        ];
    }
}
?>

我能够显示关系数据但无法插入。请在创建视图中建议如何在创建视图中显示舒适性复选框,以及如何插入 项目 project_amenity 表中的数据。< / p>

2 个答案:

答案 0 :(得分:0)

为您的项目模型添加设施属性:

public $amenities;

在您的视图中添加checkboxlist

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

如果尚未存在,则创建联结模型。

保存后在创建操作中循环输入:

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 :(得分:0)

<?=   $form->field($project, 'amenities')->inline()->checkBoxList( 
        ArrayHelper::map(Amenity ::find()->all(), 'name', 'name'),
        ['style'=>'width:500px']
        )
      ?>