【问题标题】:Doctrine 2 - Generate entities with views from databaseDoctrine 2 - 使用来自数据库的视图生成实体
【发布时间】:2013-10-30 05:16:45
【问题描述】:

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

我解释一下:

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

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

【问题讨论】:

标签: database view doctrine-orm


【解决方案1】:

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

【讨论】:

  • 你在哪里看到的?
  • 自己试试吧。 Doctrine2 模式工具不会将视图识别为表,因为它是一个状态。
  • 我明天上班试试。看最后一点,26.2.1,希望可能……docs.doctrine-project.org/en/2.0.x/reference/…
  • 实际上,不同 rdms 中的视图可能是可更新或可插入的(即它们并不总是只读的)。对于 mysql,例如:dev.mysql.com/doc/refman/5.7/en/view-updatability.html。由于使视图可更新或不可更新的不同 rdms 的标准是高度技术性的(并且依赖于),我认为 Doctrine 可能已经选择发送查询并让数据库拒绝查询...
  • @VincentPazeller,I think Doctrine may have chosen to send queries anyway and let the database reject the queries...。完全同意
【解决方案2】:

对于对答案感兴趣的人:

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

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

在 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;
    }

}

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多