Symfony getting all field names from database table
doctrine-orm, symfony
Solution
I solved this with this code :
$class = $this->em->getClassMetadata('Entity');
$fields = [];
if (!empty($class->discriminatorColumn)) {
$fields[] = $class->discriminatorColumn['name'];
}
$fields = array_merge($class->getColumnNames(), $fields);
foreach ($fields as $index => $field) {
if ($class->isInheritedField($field)) {
unset($fields[$index]);
}
}
foreach ($class->getAssociationMappings() as $name => $relation) {
if (!$class->isInheritedAssociation($name)){
foreach ($relation['joinColumns'] as $joinColumn) {
$fields[] = $joinColumn['name'];
}
}
}
return $fields;
Problem
I need to get all database table field names. I've already tried getting that using `ClassMetadata` unfortunately `getColumnNames()` doesn't return relational field names. and method `getAssociationNames()` returns names of entity properties which are not the same names and also might include fields that doesn't really exist in database when using `oneToMany` relations. As people mentioned that this may be duplicate. It's not. Getting column names in a doctrine2 entity which might be a similar problem but `getFieldNames` method doesn't return field names which has relations to other entities. So how do I get All column names from database table?