Doctrine 2 - 使用数据库中的视图生成实体

问题描述 投票:7回答:2

是否可以使用Doctrine 2从数据库生成视图?

我解释:

我的数据库包含一些我想要使用的视图,但我不知道如何生成这些视图。

在我的情况下,我有两个表和一个视图,视图选择每个表中的几列,我只想在项目的“实体”文件夹中的这个视图。

database view doctrine-orm
2个回答
8
投票

数据库视图是 不 目前由Doctrine 2支持,但它可能表现得非常糟糕。自己尝试将视图映射为实体并将其标记为@readOnly实体。


1
投票

对于对答案感兴趣的人:

我的答案基于此:How to set up entity (doctrine) for database view in Symfony 2

例如,如果您有一个包含“Id”和“Name”列的视图“A”:

在src / Name / YourBundle / Resources / config / doctrine中,使用attributs创建“A.orm.yml”,例如:

Name\YourBundle\Entity\A:
type: entity
table: A
fields:
    id:
        id: true
        type: integer
        unsigned: false
        nullable: false
        column: id
        generator:
            strategy: IDENTITY
    name:
        type: string
        length: 35
        fixed: false
        nullable: true
        column: name

之后,在Name / YourBundle / Entity / A中创建A.php:

namespace Name\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="A")
 */
class A {

    /**
    * @Id @Column(type="integer")
    */
    private $id;
    private $name;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

}

并且......您可以使用控制器调用您的视图。

© www.soinside.com 2019 - 2024. All rights reserved.