codeigniter4 / codeigniter4/CodeIgniter4

[Model] Upsert problem with data validation, allowed fields, dates defined

Đang mở
#7,286 6 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
PHP
Star
6k
Fork
2k
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
73

Mô tả

### PHP Version

7.4

### CodeIgniter4 Version

4.3.2

### CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

### Which operating systems have you tested for this bug?

Windows, Linux

### Which server did you use?

apache

### Database

MySQL (ver. 8.0.28)

### What happened?

Upsert does not work correctly with the model.
Allowed fields, validation rules, dates (created_at, updated_at) are defined within the model, this does not apply to the query.
The model should throw validation errors first!

### Steps to Reproduce

```php

class TestModel extends BaseModel
{
protected $table = 'table';
protected $allowedFields = [
'item_id', 'item_quantity'
];
protected $validationRules = [
'item_id' => 'required|is_natural_no_zero',
'item_quantity' => 'required|numeric',
];

protected $useSoftDeletes = true;
protected $protectFields = true;
protected $useTimestamps = true;
}

$model = new TestModel();

$model->upsert([
'item_id' => "string data", //validation is not correct
'item_quantity' => 99, //everything ok
'col1' => "string data", //not in allowed fields
]);

//check validation errors
$model->errors();

$model->db->getLastQuery()->getQuery();
```

### Expected Output

```sql
INSERT INTO `table` (`item_quantity`, `created_at`, `updated_at`)
VALUES (99, unix_timestamp, unix_timestamp)
ON DUPLICATE KEY UPDATE
`table`.`item_quantity` = VALUES(`item_quantity`),
`table`.`updated_at` = VALUES(`updated_at`)
```
### Anything else?
_No response_

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.