8000 [PropertyAccessor] Unable to write to singular property using setter while plural adder/remover exist · Issue #28961 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[PropertyAccessor] Unable to write to singular property using setter while plural adder/remover exist #28961
Closed
@karser

Description

@karser

Hi there, I found a bug that exists in 3.4, 4.1 and master branches.
I'll submit a PR soon.

What's happening:
That's the priorities issue: findAdderAndRemover('User', 'email') is called earlier than $this->isMethodAccessible('User', 'setEmail', 1)

The reproducer:

$user = new UserWithSingularAndPluralProps();
$accessor = PropertyAccess::createPropertyAccessor();

if ($accessor->isWritable($user, 'email')) {
    $accessor->setValue($user, 'email', 'test@email.com');
}

Here is User entity that has email and emails fields:

/**
 * Notice we don't have setter for emails
 * because we count on adder/remover
 */
class UserWithSingularAndPluralProps
{
    /** @var string|null */
    private $email;

    /** @var array */
    private $emails = [];

    /**
     * @return null|string
     */
    public function getEmail(): ?string
    {
        return $this->email;
    }

    /**
     * @param null|string $email
     */
    public function setEmail(?string $email): void
    {
        $this->email = $email;
    }

    /**
     * @return array
     */
    public function getEmails(): array
    {
        return $this->emails;
    }

    /**
     * @param string $email
     */
    public function addEmail(string $email): void
    {
        $this->emails[] = $email;
    }

    /**
     * @param string $email
     */
    public function removeEmail(string $email): void
    {
        $this->emails = array_diff($this->emails, [$email]);
    }
}

Expected result: the value is assigned to user.email.

Actual error:
Invalid argument supplied for foreach() in symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 576

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0