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

Add a new AccessType to allow for __call() #104

Closed
michaelhagedon opened this issue Apr 18, 2012 · 9 comments
Closed

Add a new AccessType to allow for __call() #104

michaelhagedon opened this issue Apr 18, 2012 · 9 comments

Comments

@michaelhagedon
Copy link

I've created an entity that uses __get() and __set() for access while still keeping properties protected for Doctrine. I've also implemented __call() to allow for more traditional-looking getters and setters, but JMSSerializerBundle can only use reflection (which bypasses my __get() implementation) or existing public methods. If there were another AccessType like "magic_call" it would be compatible with PHP's magic methods. For the moment I've overwritten the setAccessor() method on PropertyMetadata to avoid the checks for method existence, but that's obviously not right.

If anyone's wondering why I still want my __get() call to fire when serialization occurs, I've created the ability to have an "optional" getter in case I need to modify the data on the way out, and I'd like JMSSerializerBundle to respect that. Something like this:

public function __get($property) {
  $getter = '_get' . ucfirst($property);
  if (method_exists($this, $getter)) {
    return $this->$getter();
  }
  if (property_exists($this, $property) && in_array($property, $this->accessible) ) {
    return $this->$property;
  }
}
@schmittjoh
Copy link
Owner

I would use a new property metadata class, but how would you define these properties? On the class level? Or do you still have them as real properties in your class?

@michaelhagedon
Copy link
Author

I still think this is a neat idea, but for my current project I've decided to use traditional getters and setters. When I was doing this, my properties were real properties in order to be persistable by Doctrine. Thanks!

@schmittjoh
Copy link
Owner

I'm closing this for now until someone needs it.

jorns pushed a commit to jorns/JMSSerializerBundle that referenced this issue Aug 11, 2014
Potential fix for "recursion detected" issue
@TKr
Copy link

TKr commented May 7, 2015

I need it.

@exi
Copy link

exi commented Oct 1, 2015

Me too. We have a few things that need to be done on all get calls. Without __get support, we need to write the same 2 lines on >50 getters. This could easily be handled by a single __get() and __set() in the appropriate superclass

@razvanmitre
Copy link

+1

@evgemar
Copy link

evgemar commented Jun 16, 2016

I need it. A have the same problem on several projects.
For example propel delegate behaviour http://propelorm.org/documentation/behaviors/delegate.html#delegating-using-a-many-to-one-relationship is really good thing but it's impossible to use it with jms serializer.

@vincentdieltiens
Copy link

It's also interresting with Doctrine Behaviors translatable where some methods are available using __call (https://github.com/KnpLabs/DoctrineBehaviors#proxy-translations)

public function __call($method, $arguments)
{
    return $this->proxyCurrentLocaleTranslation($method, $arguments);
}

@evgemar
Copy link

evgemar commented Dec 6, 2016

This fix do serializer more simple and usable :) without reflection. You can use any name for fields but set getters and setters in config.

evgemar/serializer@13184a0
evgemar/metadata@6bcab66

P.S. it is working with Propel too, not only with Doctrine ;)

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

No branches or pull requests

7 participants