mootensai / yii2-enhanced-gii

Enhanced Yii2 Gii (generator) that generates related Models & CRUD
MIT License
203 stars 98 forks source link

Bug in update and delete in child relation #135

Open cufano opened 6 years ago

cufano commented 6 years ago

Hello,

I'm trying to test the extension with a simple parent-child relation One-to-Many. I create the models and also the controller and views. All is fine but the Update and Delete operations in child table. When I delete or change any content or add any new record in child table and Update, it generates duplicate entry in the child table.

These are my tables config:

Order Table: id int 10 Auto Increment, Not Null, Primary Key order_date date 0 Not Null qty int 6 Allow Null, NULL as Default

Products Table: id bigint 15 Auto Increment, Not Null, Primery Key order_id int 10 Not Null, Foreign Key id in Order table qty int 4 Not Null name varchar 150 Not Null

I'm also using RelationTrait, so in Update action is simple as: `public function actionUpdate($id) { $model = $this->findModel($id);

    if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}`

So it seems than in SaveAll() it is not deleting old records or adding new ones.

Reviewing the log, it seems that when Update the model it first deletes all records before insert new ones, and the Delete query is generating an error: "Impossible WHERE". The SQL is: DELETE FROM product WHERE (order_id=8) AND (id<>NULL)

It seems that the ('id'<>NULL) clause in the query is generating the error, because if I run the query in the database, no records are selected. But if I use only DELETE FROM product WHERE (order_id=8), the query is running fine.

What I'm doing wrong?

gomegs commented 6 years ago

Hi, any luck finding a fix or work-around for this issue?

alvarolordelo commented 6 years ago

@cufano @gomegs take a look at my issue, I already solved that matter and suggested a correction.

https://github.com/mootensai/yii2-enhanced-gii/issues/138