How to set up entity (doctrine) for database view in Symfony 2

前端 未结 6 1365
心在旅途
心在旅途 2020-11-30 01:51

Lets say i have a view table. And i want to get data from it to an entity. Can i (and how) create entity class to do that. (no save operation needed). I just want to display

6条回答
  •  無奈伤痛
    2020-11-30 02:41

    In addition to above anwers if you are using doctrine migrations for schema update the following configuration works perfectly.

    /**
     * @ORM\Entity(readOnly=true)
     * @ORM\Table(name="view_table_name")
     */
    class YourEntity {
        private function __construct() {}
    }
    

    Till here is te same as above answers. Here you need to configure doctrine not to bind schemas;

    doctrine:
        dbal:
            schema_filter: ~^(?!view_)~
    

    The above filter definition filters all 'view_' prefixed tables as well as views an could be extended using regex. Just make sure you have named your views with 'view_' prefix.

    But doctrine:schema:update --dump-sql still shows the views, I hope they will integrate the same filter to schema update too.

    I hope this solution would help some others.

    Source: http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#manual-tables

提交回复
热议问题