如何在Symfony 2中为数据库视图设置实体(doctrine)

use*_*963 21 php mysql symfony doctrine-orm

让我们说我有一个视图表.我希望从中获取数据到实体.我可以(以及如何)创建实体类来做到这一点.(不需要保存操作).我只想展示它们.

Ian*_*ips 27

接受的答案是正确的,但我想提供一些您可能需要考虑的其他建议:

将您的实体标记为只读.

使构造函数为私有,以便只有Doctrine可以创建实例.

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


Ale*_*aut 18

以前的答案都是正确的,但是如果你使用了学说迁移工具并且做了schema:update它就会失败......

因此,除了将实体标记为只读并使构造函数成为私有之外(在Ian Phillips答案中解释):

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

您需要将架构工具设置为在执行架构时忽略实体:更新...

为此,您只需要在bundle中创建此命令,并在ignoredEntity列表中设置yout实体:

SRC /阿克米/ CoreBundle /命令/ DoctrineUpdateCommand.php:

<?php

namespace Acme\CoreBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\Tools\SchemaTool;

class DoctrineUpdateCommand extends \Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand {

  protected $ignoredEntities = array(
      'Acme\CoreBundle\Entity\EntityToIgnore'
  );

  protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) {

    /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
    $newMetadatas = array();
    foreach ($metadatas as $metadata) {
      if (!in_array($metadata->getName(), $this->ignoredEntities)) {
        array_push($newMetadatas, $metadata);
      }
    }

    parent::executeSchemaCommand($input, $output, $schemaTool, $newMetadatas);
  }

}
Run Code Online (Sandbox Code Playgroud)

(信用到Alexandru Trandafir Catalin:从这里获得:https://stackoverflow.com/a/25948910/1442457 )

顺便说一句,这是我发现使用教条中的观点的唯一方法......我知道这是一种解决方法......如果有更好的方式我是开放的或建议的话)


Eln*_*mov 17

查询视图没有什么特别之处 - 它只是一个虚拟表.以这种方式设置您的实体表并享受:

/**
 * @ORM\Entity
 * @ORM\Table(name="your_view_table")
 */
class YourEntity {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • @ElnurAbdurrakhimov使用和ORM生成模式是Symfony推荐的做法.由于正在运行查询的性质,许多视图也没有标识符. (16认同)
  • 非常非常糟糕的做法.调用doctrine:schema:update ...并且表将被重写为简单表(不是VIEW). (14认同)
  • @ user1954544,使用ORM生成模式是一种不好的做法. (5认同)
  • @GusDeCooL,好吧,然后放一个标识符.;) (3认同)

小智 10

除了上面的anwers之外,如果您使用doctrine迁移进行架构更新,则以下配置可以完美地运行.

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

直到这里与上述答案相同.在这里,您需要配置doctrine不绑定模式;

doctrine:
    dbal:
        schema_filter: ~^(?!view_)~
Run Code Online (Sandbox Code Playgroud)

上面的过滤器定义过滤了所有'view_'前缀表以及可以使用正则表达式扩展的视图.只需确保使用"view_"前缀命名您的视图.

但是学说:schema:update --dump-sql仍然显示视图,我希望他们也会将相同的过滤器集成到模式更新中.

我希望这个解决方案可以帮助其他人.

资料来源:http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#manual-tables

  • 这至少不适用于教义2.4。架构更新尝试创建表。 (3认同)