Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you use object cast handler for Entity? #7993

Open
kenjis opened this issue Sep 30, 2023 · 4 comments
Open

How do you use object cast handler for Entity? #7993

kenjis opened this issue Sep 30, 2023 · 4 comments
Labels

Comments

@kenjis
Copy link
Member

kenjis commented Sep 30, 2023

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

The object handler seems to expect the property to be set to an array.

public static function get($value, array $params = []): object
{
return (object) $value;
}

public function testCastObject(): void
{
$entity = $this->getCastEntity();
$data = ['foo' => 'bar'];
$entity->sixth = $data;
$this->assertIsObject($entity->sixth);
$this->assertInstanceOf('stdClass', $entity->sixth);
$this->assertSame($data, (array) $entity->sixth);
}

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

        $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?

@neznaika0
Copy link
Contributor

neznaika0 commented Oct 8, 2023

@iRedds seems to have created it.

In my opinion, the conversion is not necessary. If you want to convert an array, then it should be in the database as JSON and json, json-array should be used. The concept of an object is too big, therefore it requires a custom Cast

It's probably worth adding @deprecated and replacing saving and conversion with JSON

@michalsn
Copy link
Member

michalsn commented Oct 8, 2023

I have never used it. It is useless to me in the context of a relational database. Maybe for NoSQL or other scenarios, it might have some use.

@kenjis
Copy link
Member Author

kenjis commented Oct 10, 2023

Yes, it seems useless in the context of a relational database.

But even for NoSQL, why don't you set an object to an entity, instead of an array?

@michalsn
Copy link
Member

Good point. I honestly can't imagine a valid scenario 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants