Skip to content

Bug: Not possible to set relations for an empty prop #20

@neznaika0

Description

@neznaika0

Describe the bug
If Entity have getter:

    /**
     * @return list<Tag>
     */
    public function getTags(): ?array
    {
        return $this->attributes['tags'] ?? [];
    }

in PostModel:

    public function tags(): Relation
    {
        return $this->belongsToMany(TagModel::class);
    }

The lazy loading request is not working. To work, you need to return null.

To Reproduce
Add getter for relation prop and set . Call $entity->tags

Expected behavior
An empty array is expected to be overwritten, not just null/undefined properties.

Screenshots
No.

Environment (please complete the following information):

  • PHP: 8.4.13
  • Server: cli
  • Version dev-latest
  • CodeIgniter: 4.6.3

Additional context
To fast fix:

trait HasLazyRelations
{
    public function __get(string $key)
    {
        $result = parent::__get($key);

        if ($result === null || $result === []) {
            $result = $this->handleRelation($key);
        }

        return $result;
    }
//...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions