Search code examples
phpsqlsymfonydbal

fetch_fields() using DBAL doctrine


I want to get the field name using doctrine DBAL

this is my sql query:

 $em = $this->getDoctrine()->getManager();
 $connection = $em->getConnection();
  $listcontact = $connection->prepare("select * from contact");
  $listcontact->execute();

how I can get the name of fields using DBAL


Solution

  • You can use the schema manager of Doctrine DBAL :

    // Get schema manager : 
    $sm = $connection->getSchemaManager();
    // Get fields list from table 'contact' :
    $columns = $sm->listTableColumns('contact');
    // Loop over the array to get names and other properties : 
    foreach ($columns as $column) {
        echo $column->getName() . ': ' . $column->getType() . "\n";
    }
    

    Complete doc of schema manager is here: http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/schema-manager.html