codeigniter4 / codeigniter4/CodeIgniter4

How do you use `object` cast handler for Entity?

未關閉
#7,993 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
question
主要語言
PHP
星號
6k
分支
2k
平均合併
1 天 11 小時
30 天內合併 PR
73

描述

https://codeigniter4.github.io/CodeIgniter4/models/entities.html#property-casting

The `object` handler seems to expect the property to be set to an array.
https://github.com/codeigniter4/CodeIgniter4/blob/0cd39937d7a8dbd8074237596024adce0fe3c2af/system/Entity/Cast/ObjectCast.php#L22-L25
https://github.com/codeigniter4/CodeIgniter4/blob/0cd39937d7a8dbd8074237596024adce0fe3c2af/tests/system/Entity/EntityTest.php#L439-L449

However, when setting an array, we cannot save that Entity to the database.

```php
$entity = new class () extends Entity {
protected $casts = [
'id' => 'int',
'active' => 'int-bool',
'memo' => 'object', // Use `object` handler
];
};
$model = new class () extends Model {
protected $table = 'users';
protected $allowedFields = [
'username', 'active', 'memo',
];
protected $useTimestamps = true;
};
$entity->fill(['username' => 'johnsmith', 'active' => false, 'memo' => ['foo', 'bar']]);
$model->save($entity); // CodeIgniter\Database\Exceptions\DatabaseException : Operand should contain 1 column(s)
```

How is this handler used in the first place?

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。