2

I am currently retrieving the class name of my entities to save changes into a log. This happens in a listener:

In my service layer:

$product = $line->getProduct();

$product->setAvailability($product->getAvailability() - $line->getAmount());
$em->persist($product);

the problem is that by doing following in a listener:

$className = join('', array_slice(explode('\\', get_class($entity)), -1));
$modification->setEntidad($className);

The $className that is set into the modification is miomioBundleEntityProductoProxy.

How can I get the real class name for my entity, and not the proxy class name?

2
  • I neither have an idea of what your problem is nor what your question is. Please consider rephrasing your question so it can be understand. Commented Mar 18, 2013 at 16:11
  • @Sgoettschkes I've rewritten the question Commented Mar 18, 2013 at 20:01

2 Answers 2

2

As the proxy class always extends from the real entity class:

class <proxyShortClassName> extends \<className> implements \<baseProxyInterface>

then, you can get it with class_parents() function:

if ($entity instanceof \Doctrine\Common\Proxy\Proxy) {
    $class = current(class_parents($entity)); // get real class
}

Especially useful when you don't have access to EntityManager instance.

Sign up to request clarification or add additional context in comments.

Comments

1

The fact that you receive a proxy name when calling get_class on a proxy is quite normal, since proxies are a required concept to let the ORM and lazy loading of associations work.

You can get the original class name by using following API:

$realClassName = $entityManager->getClassMetadata(get_class($object))->getName();

Then you can apply your own transformations:

$normalizedClassName = join('', array_slice(explode('\\', $realClassName), -1));

$modificacion->setEntidad($normalizedClassName);

2 Comments

it's faster to use ClassUtils::getRealClass($className)
That's not going to retrieve the class name in all cases. You shouldn't do that manually as it's an implementation detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.