7

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

我解释:

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

在我的例子中,我有两个表和一个视图,视图在每个表中选择了几列,我只想在项目的“实体”文件夹中使用这个视图。

4

2 回答 2

8

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

于 2013-10-21T20:53:33.630 回答
0

对于对答案感兴趣的人:

我的回答基于: How to setup entity (doctrine) for database view in Symfony 2

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

在 src/Name/YourBundle/Resources/config/doctrine,创建具有如下属性的“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;
    }

}

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

于 2013-10-22T09:29:35.643 回答