priyanka ahire priyanka ahire - 4 years ago 341
PHP Question

How to delete Foreigen key constraint from table in yii2?

In actionDelete I have write query for get fk from all table and create activeDataProvider.and write a foreach loop for geting one by one element.

$query = new \yii\db\Query;
$query->select('practiceCode')->from('member','plan','offer','appointment','product','incentive','clinic','complaint')
->where(['practiceCode' => $model->practiceCode])->all();
$query->createCommand();
$dataProvider= new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
$models = $dataProvider->getModels();
if(count($models) >= 1) {
$memberModel = new Member();
foreach ($models as $k) {
$k['deleted'] = 'Y';
//$memberModel->save();

$connection->createCommand()->update('member', ['deleted' => 'Y'], ['practiceCode' => $models['practiceCode']])->execute();


Now it's work Doing same think for all table one by one

}
return $this->redirect(['index']);


I am troubling with how save this Flag 'Y' in all table where this practiceCode as foreign key.Please help how to do....Thanks in advance

Answer Source

i.e : $oModel = ModelName::find()->with('originaltable_name')->where(['practiceCode' => $your_value])->all(); you will select your records and delete. you will define your relation using with()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download