Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate entities from database view with doctrine and symfony2

I am trying to generate entities from database using standard console commands as described in Symfony2 documentation here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html.

php app/console doctrine:mapping:convert --from-database --force yml "src/My/HomeBundle/Resources/config/doctrine/metadata/orm"
php app/console doctrine:mapping:import MyHomeBundle yml
php app/console doctrine:generate:entities MyHomeBundle

After this, all tables are generated correctly. The problem is that this won't generate entities for database views. When I add yml files myself into src/My/HomeBundle/Resources/config/doctrine/metadata/orm for example:

UserInGroup:
  type: entity
  table: user_in_group_view
  fields:
    id:
      id: true
      type: integer
      unsigned: false
      nullable: false
      generator:
        strategy: IDENTITY
    userId:
      type: integer
      unsigned: false
      nullable: false
      column: user_id
    userGroupId:
      type: integer
      unsigned: false
      nullable: false
      column: user_group_id
  lifecycleCallbacks: {  }

I get this exception when running php app/console doctrine:generate:entities MyHomeBundle:

Notice: Undefined index: My\HomeBundle\Entity\UserInGroup in C:\Users\ThisIsMe\Projects\SymfonyTestProject\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\AbstractFileDriver.php line 121

Similar question was posted here: How to set up entity (doctrine) for database view in Symfony 2

I know I can create Entity class, but I was hoping that I could get this generated so if I change my view, I could just regenerate entity classes. Any suggestions?

like image 827
Kosta Avatar asked Mar 23 '12 03:03

Kosta


1 Answers

Now you create your orm files only. You need to follow 2 more steps. I will give you the complete steps from begining.

Before doing this delete all yml files in your orm directory that you had created early.

I hope MyHomeBundle is your bundle name

1).php app/console doctrine:mapping:convert yml ./src/My/HomeBundle/Resources/config/doctrine --from-database --force 

  Symfony2 generate entity from Database

 2).php app/console doctrine:mapping:import MyHomeBundle yml

 3).php app/console doctrine:generate:entities MyHomeBundle

Hope this helps you.

like image 61
Asish AP Avatar answered Oct 16 '22 04:10

Asish AP