Skip to content

[PropertyAccess] [For reference only] Allow custom property accessors #38515

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

Closed
wants to merge 1 commit into from

Conversation

lrlopez
Copy link
Contributor

@lrlopez lrlopez commented Oct 11, 2020

Disclaimer: this is just a relaunch of PR #22190 targeting "5.x" instead of "master". @fabpot asked to create this PR because he's not able to switch the target repository as the old PR is already closed. Take into account that this code was intended to be merged into 3.4 a loooong time ago...

Q A
Branch 5.x
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #18016, #5013, #9336, #5219, among others
License MIT
Doc PR symfony/symfony-docs#6400

Here is the first of the two PR related to #18016. This implements changes on the PropertyAccess component and will be followed by another one leveraging the new features into the FrameworkBundle.

Now it is possible to override the property accessors (getters, setters, adders and removers) using annotations or YAML config files. Metadata is automatically cached in the pool configured for the property path cache.

Some examples of how to use the new feature:

Annotations:

/**
 * @PropertyAccessor(getter="getCurrentValue", setter="updateValue")
 */
protected $value;

/**
 * @PropertyAccessor(adder="joinMember", remover="detachMember")
 */
protected $members;

public function getCurrentValue()
{
    return $this->value;
}

public function updateValue($newValue)
{
    $this->value = $newValue;
    return $this;
}

public function joinMember($newMember)
{
    // Add $newMember to the collection
}

public function detachMember($oldMember)
{
    // Remove $oldMember from the collection
}

// Notice that "calculated" is not a real property!
/**
 * @GetterAccessor(property="calculated")
 */
public function getCalculated()
{
    // whatever...
}

// You can omit 'property' in method annotations
/**
 * @SetterAccessor("calculated")
 */
public function setCalculated($calculated)
{
    // whatever...
}

YAML:

'AppBundle\Entity\Example':
  properties:
    value:
      getter: getCurrentValue
      setter: updateValue
    members:
      adder: joinMember
      remover: detachMember
    calculated:
      getter: getCalculated
      setter: setCalculated

XML:

<?xml version="1.0" ?>

<property-access xmlns="http://symfony.com/schema/dic/property-access-mapping"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://symfony.com/schema/dic/property-access-mapping http://symfony.com/schema/dic/property-access-mapping/property-access-mapping-1.0.xsd">

    <class name="AppBundle\Entity\Example">
        <property name="value" getter="getCurrentValue" setter="updateValue" />
        <property name="members" adder="joinMember" remover="detachMember" />
        <property name="calculated" getter="getCalculated" setter="setCalculated" />
    </class>

</property-access>

@fabpot
Copy link
Member

fabpot commented Jul 25, 2022

I really like this feature but as nobody ever resumed the work, I'm going to close it now :(

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

Successfully merging this pull request may close these issues.

4 participants