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

前端 未结 6 1366
心在旅途
心在旅途 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:35

    Both the previous answers are correct, but if you use the doctrine migration tool and do a schema:update it will fail...

    So, in addition to marking the entity as read only and making the constructor private (explained in Ian Phillips answer):

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

    You would need to set the schema tool to ignore the entity when doing a schema:update...

    In order to do that you just need to create this command in your bundle, and set yout entity in the ignoredEntity list:

    src/Acme/CoreBundle/Command/DoctrineUpdateCommand.php:

    getName(), $this->ignoredEntities)) {
            array_push($newMetadatas, $metadata);
          }
        }
    
        parent::executeSchemaCommand($input, $output, $schemaTool, $newMetadatas);
      }
    
    }
    

    (credit to Alexandru Trandafir Catalin: obtained from here: https://stackoverflow.com/a/25948910/1442457)

    BTW, this is the only way I found to work with views from doctrine... I know it is a workaround... If there is a better way I am open or suggestions)

提交回复
热议问题