I have this tabular form / Data array as shown in the picture:
7199
I need to update the company_id and is_mapped_to_inspector of the inspection_mapping table(Model name is InspectionMapping) based on two parameters: zone_id and is_mapped_to_inspector.
-
Instead of updating just the 3 records, it updates all the 18 records.
-
It updates the records with the same company_id and is_mapped_to_inspector, as shown below:
7200
instead of
7201
Controller
public function actionZonalmapping()
{
$user = Yii::$app->getid->getId();
$zonalSession = Yii::$app->session->get('zonal_id');
$ZonalMaster = ZonalAdministrator::find()->andWhere(['zonal_id' => $zonalSession])->one();
$ZonalInfo = ZonalAdministrator::findOne($ZonalMaster->zonal_id);
$zonalZone = Zone::findOne($ZonalMaster->zone_id);
$zone = $ZonalInfo->zone_id;
$inspector = new Inspector();
// die(var_dump($inspector));
$inspectionmapping = new InspectionMapping();
$inspectionmapping=InspectionMapping::find()->andWhere(['zone_id' => $zone, 'is_mapped_to_zone' => 1])->all();
// die(var_dump($inspectionmapping));
if(isset($_POST['submit'])){
if($_POST['submit']=='Save'){
if(isset($_POST['Inspector'])){
$inspectionmapping = new InspectionMapping();
foreach ($_POST['Inspector']['inspection_mapping'] as $key=>$value)
{
$inspectionmapping=InspectionMapping::find($key)->All();
// die(var_dump($inspectionmapping));
foreach ($inspectionmapping as $valuex)
{
$valuex->company_id=$value['company_id'];
// die(var_dump($inspectionmapping));
$valuex->is_mapped_to_inspector= 1;
$valuex->save(false);
}
}
}
}
return $this->redirect(['index']);
}
else{
return $this->render('zonalmapping', [
'inspector' => $inspector,
'inspectionmapping' => $inspectionmapping,
]);
}
}
View
<div class="col-xs-12 col-lg-12">
<!--<div class="inspector-update"> -->
<!--</div>-->
<!--</div>-->
<div class="mapping-form">
<?php $form = ActiveForm::begin(['id' => 'inspection-form',
'enableAjaxValidation' => false,]); ?>
<h1>Building(s) Mapping to Inspector(s)</h1>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Mapping: Building(s) to Inspector(s)</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Building</th>
<th>Zone</th>
<th>Fee</th>
<th>Tariff Group</th>
<th>Inspector</th>
</tr>
</thead>
<tbody>
<?php
$user = Yii::$app->getid->getId();
$zonalSession = Yii::$app->session->get('zonal_id');
$ZonalMaster = ZonalAdministrator::find()->andWhere(['zonal_id' => $zonalSession])->one();
$ZonalInfo = ZonalAdministrator::findOne($ZonalMaster->zonal_id);
$zonalZone = Zone::findOne($ZonalMaster->zone_id);
$zone = $ZonalInfo->zone_id;
$i = 1;
$key = 0;
foreach ($inspectionmapping as $cvalue)
{
?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $cvalue->building->building_name; ?></td>
<td><?php echo $cvalue->zone->zone_name; ?></td>
<td><?php echo $cvalue->fee->value; ?></td>
<td><?php echo $cvalue->tariff->tariff_group_detail; ?></td>
<td>
$name="Inspector[inspection_mapping][$cvalue[inspection_mapping_id]][company_id]";
$yn=array("$cvalue[company_id]"=>array('selected'=>true));
echo $form->field($inspector, 'company_id')->dropDownList(ArrayHelper::map(\app\modules\contractors\models\Inspector::find()->andWhere(['zone_id' => $zone])->all(),'company_id','inspector_name'), ['name'=>$name,'options'=>$yn])->label(false);
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<!-- /.col -->
</div>
<!-- /.row -->
<div class="form-group">
<div class="col-xs-12 col-sm-2 col-lg-2">
<input type="submit" class="btn btn-primary btn-block btn-lg" name="submit" value="Save"/>
</div>
</div>
</section>
<!-- /.content -->
<?php ActiveForm::end(); ?>
</div> <!-- /.End of Inspection Sheet -->
</div> <!-- /.End of col-xs-12 col-lg-12 -->
Model
class InspectionMapping extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'inspection_mapping';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['building_id', 'tariff_id', 'fee_id', 'company_id', 'zone_id', 'is_inspected', 'is_paid', 'is_booked', 'is_mapped_to_inspector', 'is_mapped_to_zone'], 'integer'],
[['mapping_date'], 'required'],
[['mapping_date'], 'safe']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'inspection_mapping_id' => 'Inspection Mapping ID',
'building_id' => 'Building ID',
'tariff_id' => 'Tariff ID',
'fee_id' => 'Fee ID',
'company_id' => 'Company ID',
'zone_id' => 'Zone ID',
'is_inspected' => 'Is Inspected',
'is_paid' => 'Is Paid',
'is_booked' => 'Is Booked',
'is_mapped_to_zone' => 'Is Mapped To Zone',
'is_mapped_to_inspector' => 'Is Mapped To Inspector',
'mapping_date' => 'Mapping Date',
];
}
public function getBuilding()
{
return $this->hasOne(Building::className(), ['building_id' => 'building_id']);
}
public function getTariff()
{
return $this->hasOne(BuildingTariff::className(), ['tariff_id' => 'tariff_id']);
}
public function getFee()
{
return $this->hasOne(Fee::className(), ['fee_id' => 'fee_id']);
}
public function getZone()
{
return $this->hasOne(Zone::className(), ['zone_id' => 'zone_id']);
}
public function getInspector()
{
return $this->hasOne(Inspector::className(), ['company_id' => 'company_id']);
}
public function getBuildingName()
{
return $this->building->building_name;
}
}
Please its not updating the records correctly. What do I do