9

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

These are the same steps I've described in my question and this generates entities for all tables not for views. Difference is in destination path for generated yml files. I've tried your solution just in case but it not solve the problem only tables are turned into entities. I know that earlier versions of Doctrine (and Symfony) could generate entities out of views without any problems.
0

Got the same issue, i use xml instead of yml but must be the same.

Check in your orm entity if the name include the correct route, exemple:

<entity name="Myapp\MyrBundle\Entity\MyEntity" table="myentity">

Because when i generate my orm from database the name was like that:

<entity name="MyEntity" table="myentity">

So doctrine didn't understand the right path.

Hope i'm clear and this will help you!

5 Comments

Checked. I thought of that and tried different combination without any luck. But thanks anyway.
can you past the entity UserInGroup ?
the thing is that I didn't want to create entity on my own, but to make Doctrine do that for me. Finally, I've created entity and now working with it, but the question still remains.
Ok but the thing is if you did the doctrine:mapping:import you should have entities in your entity folder. The generate:entities just generate the getters and setters of the entities, so maybe the namespace or class name of the entity is wrong (that would explain your error) but if you manage to get your entities that's great.
Exactly! But they're not there, and that's the problem and my question. Note that entities for database tables are generated correctly - the problem is with database views.
0

I know it's an old question but I found the trick (Symfony 6) to generate entities from SQL view (I use a SQL server database).

So I modified two methods of the SQLServerPlatform.php file of the doctrine bundle.

public function getListTablesSQL()
{
    // "sysdiagrams" table must be ignored as it's internal SQL Server table for Database Diagrams
    // Category 2 must be ignored as it is "MS SQL Server 'pseudo-system' object[s]" for replication
    return 'SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects'
        . " WHERE type = 'V' AND name != 'sysdiagrams' AND category != 2 ORDER BY name";
}

I changed the where condition parameter 'U' by 'V' for view.

Same operation for the method getListTableColumnsSQL(), we change here the 'U' parameter of the where condition by 'V'.

public function getListTableColumnsSQL($table, $database = null)
{

    return "SELECT    col.name,
                      type.name AS type,
                      col.max_length AS length,
                      ~col.is_nullable AS notnull,
                      def.definition AS [default],
                      col.scale,
                      col.precision,
                      col.is_identity AS autoincrement,
                      col.collation_name AS collation,
                      CAST(prop.value AS NVARCHAR(MAX)) AS comment -- CAST avoids driver error for sql_variant type
            FROM      sys.columns AS col
            JOIN      sys.types AS type
            ON        col.user_type_id = type.user_type_id
            JOIN      sys.objects AS obj
            ON        col.object_id = obj.object_id
            JOIN      sys.schemas AS scm
            ON        obj.schema_id = scm.schema_id
            LEFT JOIN sys.default_constraints def
            ON        col.default_object_id = def.object_id
            AND       col.object_id = def.parent_object_id
            LEFT JOIN sys.extended_properties AS prop
            ON        obj.object_id = prop.major_id
            AND       col.column_id = prop.minor_id
            AND       prop.name = 'MS_Description'
            WHERE     obj.type = 'V'
            AND       " . $this->getTableWhereClause($table, 'scm.name', 'obj.name');
}

Maybe this will help someone.

Comments

-1

As you can see here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html the reverse engineering process from db to entity is not fully implemented yet:

"As the Doctrine tools documentation says, reverse engineering is a one-time process to get started on a project. Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities."

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.