来自另一个表yii2的复选框并保留选中的值

use*_*282 2 yii2

我有三张桌子

--
-- Table structure for table `tbl_ticket`
--

CREATE TABLE IF NOT EXISTS `tbl_ticket` (
`id` int(9) NOT NULL,
  `complain_id` varchar(250) CHARACTER SET latin1 NOT NULL,
  `section_id` varchar(250) CHARACTER SET latin1 NOT NULL,
  `location_id` varchar(250) CHARACTER SET latin1 NOT NULL,
  `status` varchar(250) CHARACTER SET latin1 NOT NULL,
  `remarks` varchar(250) CHARACTER SET latin1 NOT NULL,
  `r_date` datetime NOT NULL,
  `d_date` datetime NOT NULL,
  `hd_user_username` varchar(250) CHARACTER SET latin1 NOT NULL,
  `hd_user_email` varchar(250) CHARACTER SET latin1 NOT NULL,
  `description` varchar(250) NOT NULL,
  `attachment` varchar(250) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_ticket`
--
ALTER TABLE `tbl_ticket`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_ticket`
--
ALTER TABLE `tbl_ticket`
MODIFY `id` int(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;

--
-- Table structure for table `tbl_ticket_complain`
--

CREATE TABLE IF NOT EXISTS `tbl_ticket_complain` (
`id` int(9) NOT NULL,
  `ticket_id` varchar(250) NOT NULL,
  `complain_id` varchar(250) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_ticket_complain`
--
ALTER TABLE `tbl_ticket_complain`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_ticket_complain`
--
ALTER TABLE `tbl_ticket_complain`
MODIFY `id` int(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
Run Code Online (Sandbox Code Playgroud)

   --
-- Table structure for table `tbl_complain_type`
--

CREATE TABLE IF NOT EXISTS `tbl_complain_type` (
`id` int(9) NOT NULL,
  `section_id` varchar(250) NOT NULL,
  `complains` varchar(250) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_complain_type`
--
ALTER TABLE `tbl_complain_type`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_complain_type`
--
ALTER TABLE `tbl_complain_type`
MODIFY `id` int(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20;
Run Code Online (Sandbox Code Playgroud)

我想显示tbl_complain_type在视图中显示为复选框的内容,即id和抱怨.tbl_ticket将有多个tbl_complain_type .id as values and this is related through the table tbl_ticket_complain.ticket_id = tbl_ticket.id.如何在CRII的Yii2视图中完成此操作?

在控制器中我已经像这样做了actionCreate

$model = new Ticket();
        $ticket_complain=new TicketComplain();
        $complain_type=ComplainType ::find ()->all();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                    'complain_type'=>$complain_type,
                    'ticket_complain'=>$ticket_complain
            ]);
        }
Run Code Online (Sandbox Code Playgroud)

并在视图中显示它们作为复选框我添加了这样的代码

<?php 

$model->complains = $complain_type;

$list = $complain_type;

$options = \yii\helpers\ArrayHelper::map($list, 'id', 'complains');


echo $form->field($model, 'complains')->checkboxList($options);

?>
Run Code Online (Sandbox Code Playgroud)

我面临的问题是保留复选框值并保存/更新值.另外我不确定是否使用checkBox而不是checkboxList,即

$complains = [];
      if($complain_type ) {
         foreach($complain_type  as $complain) {
             $complains[] = $complain->complains;
             $id[]= $complain->id;
             echo $form->field($ticket_complain, 'id')
             ->checkBox(['label' => $complain->complains, 'uncheck' => null, 'selected' => true]);
         }
      }
Run Code Online (Sandbox Code Playgroud)

或者让我简化一下

如何将模型值设置为从数据库中选择的复选框,并基于提交时提交的值?

这个sql可以演示这三个表之间的关系

SELECT  `tbl_ticket`.* , tbl_complain_type.complains  FROM tbl_ticket JOIN
tbl_ticket_complain ON tbl_ticket.id =tbl_ticket_complain.ticket_id JOIN
tbl_complain_type ON tbl_complain_type.id=tbl_ticket_complain.complain_id
Run Code Online (Sandbox Code Playgroud)

Bal*_*ath 7

在model\app\models\Ticket.php中添加以下变量,

class Ticket extends \yii\db\ActiveRecord
{
    public $complains_field; //A custom variable to hold the value of checkboxlist

    /*Remaining contents of Ticket model */

    public function rules()
    {
        return [
            [['complains_field'], 'safe']
        ];
    }

    public function getComplains() //Relation between ticket & ticket_complain table
    {
        return $this->hasMany(TicketComplain::className(), ['ticket_id' => 'id']);
    }

    public function afterSave($insert, $changedAttributes){
        \Yii::$app->db->createCommand()->delete('tbl_ticket_complain', 'ticket_id = '.(int) $this->id)->execute(); //Delete existing value
        foreach ($this->complains_field as $id) { //Write new values
            $tc = new TicketComplain();
            $tc->ticket_id = $this->id;
            $tc->complain_id = $id;
            $tc->save();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制器上的actionCreate()中

$model = new Ticket();
$complain_type= ComplainType::find()->all();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
    return $this->redirect(['view', 'id' => $model->id]);
} 
else {
    return $this->render('create', [
        'model' => $model,
        'complain_type'=>$complain_type
    ]);
}
Run Code Online (Sandbox Code Playgroud)

在控制器上的actionUpdate()中

$model = $this->findModel($id);
$complain_type = ComplainType::find()->all();

//Retrieve the stored checkboxes
$model->complains_field = \yii\helpers\ArrayHelper::getColumn(
    $model->getComplains()->asArray()->all(),
    'complain_id'
);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
    return $this->redirect(['view', 'id' => $model->id]);
} 
else {
    return $this->render('create', [
        'model' => $model,
        'complain_type'=>$complain_type
    ]);
}
Run Code Online (Sandbox Code Playgroud)

在视图中

<?php 
$options = \yii\helpers\ArrayHelper::map($complain_type, 'id', 'complains');
echo $form->field($model, 'complains_field')->checkboxList($options, ['unselect'=>NULL]);
?>
Run Code Online (Sandbox Code Playgroud)