9

I already know how to get field names for entity, but how to get class names for all associations also?

$em->getClassMetadata('Product')->getFieldNames();

This gets class property only, but I expect to get association names inside nested array, for example if I have customer associated to product I would like to get all customer class property names too.

1
  • can't you iterate over the associations given and request their meta data as well? Apr 16, 2014 at 13:31

4 Answers 4

14

You can get the associated field names as well then merge them

$properties = $em->getClassMetadata('YourBundle:Product')->getFieldNames();
$output = array_merge(
              $properties, 
              $em->getClassMetadata('YourBundle:Product')->getAssociationNames()
);
4

One liner:

array_keys($entityManager->getMetadataFactory()->getMetadataFor($entity)->reflFields)
0

Just a little hint.

I would prefer restructuring the way your organize your schema in order to properly retrieve all associated fields. For instance, according to this line of your question "if I have customer associated to product", I believe you have at least one-to-one or one-to-many relationship between customer and product. If you declare these relationships in your ORM, if you use YAML or annotations or any preferred/give way, you will be able to retrieve field names with a standard DQL query / or get methods.

The point to taken from here is, when you are expecting associating fields to be retrieved, it's best if you establish a solid database schema by providing relevant relationships at first place. This will reduce the job you have to do by half and will increase the performance in system/coding practice in long run.

Hope this helps, Cheers!

0

I ended up merging them into one array by getting first each association name and then field names for each name.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.