Symfony getting all field names from database table

symfony doctrine
doctrine orm

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?

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)) {
    foreach ($class->getAssociationMappings() as $name => $relation) {
        if (!$class->isInheritedAssociation($name)){
            foreach ($relation['joinColumns'] as $joinColumn) {
                $fields[] = $joinColumn['name'];
    return $fields;

Symfony getting all field names from database table, I need to get all database table field names. I've already tried getting that using ClassMetadata unfortunately getColumnNames() doesn't return relational field  These are parameters that you can configure: db_table (default sessions): The name of the session table in your database; db_username: (default: '') The username used to connect when using the PDO configuration (when using the connection based on the DATABASE_URL env var, it overrides the username defined in the env var).

In Doctrine the DBAL component is made to handle database-level tasks. It is also able to list all columns of a database table. The DBAL component operates on the database level wich means it does not look at any entity metadata. Assuming you have an EntityManager instance, you can get the column names like this:

// $em is your Doctrine\ORM\EntityManager instance
$schemaManager = $em->getConnection()->getSchemaManager();
// array of Doctrine\DBAL\Schema\Column
$columns = $schemaManager->listTableColumns($tableName);

$columnNames = [];
foreach($columns as $column){
    $columnNames[] = $column->getName();
// $columnNames contains all column names

The documentation for the schema manager can be found here:

How to Generate Entities from an Existing Database (Symfony 2.8 , How to Generate Entities from an Existing Database: When starting work on a documentation says, reverse engineering is a one-time process to get started on a project. Metadata files describe the entity class to generate based on table fields. class BlogComment { /** * @var integer $id * * @ORM\Column(name="​id",  Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases.These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

I solved this problem by using raw sql query. In my case as I was needing all the data from the table for the data export I used the query "SELECT * FROM $tableName" and then I used array_keys function with the first result row. In case I need to get entity field names representing that column you can use:


It doesn't take arguments as array so I have to foreach all db column names with this function but with this

Databases and the Doctrine ORM (Symfony 3.3 Docs), Instead, Doctrine allows you to fetch entire objects out of the database, and to persist your database schema, simply map to a different table name or column name. Your database now has a fully-functional product table with columns that  DoctrineMigrationsBundle¶. Database migrations are a way to safely update your database schema both locally and on production. Instead of running the doctrine:schema:update command or applying the database changes manually with SQL statements, migrations allow to replicate the changes in your database schema in a safe manner.

Building PHP Applications with Symfony, CakePHP, and Zend Framework, Note that you replace <example> with your module name. you can store attachment files directly under a file system path or in a database. Assume that your Attachments table structure looks as follows: field name type id varchar(32​) content BLOB You can get attachment's content using Doctrine, as shown in 186  -- Query to Get Column Names From Table in SQL Server USE [SQL Tutorial] GO SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'NewCustomers' OUTPUT You can use the below query to get all the information about the Table

Defining Models, Doctrine Doctrine1 ORM Documentation: Defining Models. Table of Contents For example if you want to change the name of the database field all you need  The most common relationship, mapped in the database with a foreign key column (e.g. a category_id column on the product table). This is actually just one association type, but seen from the two different sides of the relation.

Getting Started with Doctrine, How to install and configure Doctrine by connecting it to a database; Mapping When creating entity classes, all of the fields should be protected or private The top-level entity definition specifies information about the class and table name. For the form, new, and edit views: Use an associative array to group fields with the group name as a key, or NONE for a group with no name. The value is still an array of ordered column names. Be careful to list all the required fields referenced in your form class or you may have some unexpected validation errors (see Chapter 10). Custom Fields

  • Are you looking for cross platform solution or some DBMS specific?
  • possible duplicate of Getting column names in a doctrine2 entity
  • As I mentioned in my question it doesn't get everything I need. It's not a duplicate. I saw that question and it does not solve my problem
  • Cross platform solution would be ideal. otherwise MySQL would work for now
  • Perhaps you should check that 'joinColumns' key really exists in $relation. There are cases where it's doesn't.
  • Not sure it is a super good idea to delete elements of an array while you are iterating over it....
  • Works fine - does contain the relation column name.
  • Well, that's a rather poor solution. First, native SQL is really bad, especially to get meta information. Second, if you really want to do native SQL, why do you load the entire table? Ever heard of LIMIT? Not to mention SHOW COLUMNS.
  • As I said I was needing all the data anyway for export. and I don't see any reason why native SQL should be bad in this case.