codeigniter4 / codeigniter4/CodeIgniter4

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

Abierto
#7,993 4 comentarios 0 reacciones 0 asignados Ver en GitHub
question
Lenguaje dominante
PHP
Estrellas
6k
Forks
2k
Merge medio
1 d 11 h
PR fusionados (30 d)
73

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.