Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
另一个表yii2中的复选框并保留选中值_Yii2 - Fatal编程技术网

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

另一个表yii2中的复选框并保留选中值,yii2,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

我有三张桌子

--
-- 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;

我想显示
tbl\u投诉类型的内容,即id和投诉在视图中显示为复选框。
tbl\U票证将有多个tbl\U投诉类型
.id作为值,这通过表tbl\U票证投诉
进行关联。票证id=
tbl\U票证
.id。如何在CRUD的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
            ]);
        }
<?php 

$model->complains = $complain_type;

$list = $complain_type;

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


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

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

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

$model->complains = $complain_type;

$list = $complain_type;

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


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

?>
或者让我简化一下

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

这三个表之间的关系可以通过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

在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();
        }
    }
}
在控制器上的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
    ]);
}
$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
    ]);
}
在控制器上的actionUpdate()中

$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
    ]);
}
$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
    ]);
}
鉴于

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

控制器中的所有逻辑都已移动到模型中。在控制器中,只需要执行load()和save()


/**
*特别适用于表单扩展模型。没有良好的做法自动加载复选框
*每个加载模型的记录。
*/
类CwbrForm扩展了Cwbr
{
/**@var int[]*/
公共$chkList=[];
公共$oldchlist;
公共职能规则()
{
返回数组_merge(父::规则()[
['chkList','each','rule'=>['integer']]
]);
}
/**
*@return\yii\db\ActiveQuery
*/
公共函数getCwbrChk()
{
返回$this->hasMany(CwbrChk::className(),['cwbru ide_id'=>'id']);
}
公共函数加载($data,$formName=null)
{
$this->oldchkList=$this->chkList;
如果(!parent::load($data,$formName)){
返回false;
}
如果(!$this->chkList){
$this->chkList=[];
}
返回true;
}
保存后的公共功能($insert,$changedAttributes)
{
父项::afterSave($insert,$changedAttributes);
foreach(数组_diff($this->chkList,$this->oldchlist)作为$newChkId){
$chk=新的CwbrChk();
$chk->cwbr\u id=$this->id;
$chk->chk_id=$newChkId;
$chk->save();
}
CwbrChk::deleteAll([
'chk_id'=>array_diff($this->oldChkList,$this->chkList),
“cwbr\u id”=>$this->id
]);
}
公共功能后发现()
{
父::afterFind();
$this->chkList=ArrayHelper::getColumn($this->cwbrChk,'chk_id',false);
}
}
看法

$options=[1=>'One',2=>'Two'];
echo$form->field($model,'chkList')->checkbox列表($options,['unselect'=>NULL]);

您能帮我了解一下您的表格结构吗??我需要知道tbl票上的投诉id。这与tbl_投诉类型有关吗?它与tbl_投诉类型没有任何关系。据我所知,您有多个复选框。您希望将选定值保存为数据库中以逗号分隔的值,然后在更新时将其显示为选定值,该值是在更新或创建时选定的。对吗?您在保存数据库时不会遇到问题,但在更新时不会保留选定的值。我说的对吗?非常感谢!一个小的补充:我认为“检索存储的复选框”应该在模型的后发现中,而不是控制器中