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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8377671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:34:31  来源:igfitidea点击:

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

phpmysqlsymfonydoctrine-orm

提问by user1063963

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 them.

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

采纳答案by Elnur Abdurrakhimov

There is nothing special in querying a view — it's just a virtual table. Set the table of your entity this way and enjoy:

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

/**
 * @ORM\Entity
 * @ORM\Table(name="your_view_table")
 */
class YourEntity {
    // ...
}

回答by Ian Phillips

The accepted answer is correct, but I'd like to offer some additional suggestions that you might want to consider:

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

Mark your entity as read-only.

将您的实体标记为只读。

Make the constructor private so that only Doctrine can create instances.

将构造函数设为私有,以便只有 Doctrine 可以创建实例。

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

回答by Alex Mantaut

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

前面的两个答案都是正确的,但是如果您使用学说迁移工具并执行schema:update它会失败...

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

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

/**
 * @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:

为此,您只需要在您的包中创建此命令,并在 ignoreEntity 列表中设置您的实体:

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

src/Acme/CoreBundle/Command/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);
  }

}

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

(归功于 Alexandru Trandafir Catalin:从这里获得: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)

顺便说一句,这是我发现的唯一一种处理教义观点的方法......我知道这是一种解决方法......如果有更好的方法我很开放或建议)

回答by bekco

In addition to above anwers if you are using doctrine migrations for schema update the following configuration works perfectly.

除了以上答案之外,如果您使用学说迁移进行架构更新,以下配置也可以完美运行。

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

Till here is te same as above answers. Here you need to configure doctrine not to bind schemas;

直到这里与上述答案相同。这里需要配置不绑定模式的学说;

doctrine:
    dbal:
        schema_filter: ~^(?!view_)~

The above filter definition filters all 'view_' prefixed tables as well as views an could be extended using regex. Just make sure you have named your views with 'view_' prefix.

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

But doctrine:schema:update --dump-sql still shows the views, I hope they will integrate the same filter to schema update too.

但是doctrine:schema:update --dump-sql 仍然显示视图,我希望它们也将相同的过滤器集成到架构更新中。

I hope this solution would help some others.

我希望这个解决方案能帮助其他人。

Source: http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#manual-tables

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

回答by sebastien hupin

In addition to above answer, I have mixed some of your example code to extend the DoctrineUpdateCommand

除了上面的答案,我还混合了你的一些示例代码来扩展 DoctrineUpdateCommand

This is my DoctrineUpdateCommand:

这是我的 DoctrineUpdateCommand:

class DoctrineUpdateCommand extends UpdateSchemaDoctrineCommand{
   protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) {
      $container = $this->getApplication()->getKernel()->getContainer();  

     $filterExpr = $container->get('doctrine')->getEntityManager()->getConnection()->getConfiguration()->getFilterSchemaAssetsExpression();
     $emptyFilterExpression = empty($filterExpr);

     /** @var $newMetadatas \Doctrine\ORM\Mapping\ClassMetadata */
     $newMetadatas = array();

     foreach ($metadatas as $metadata) {
        if(($emptyFilterExpression||preg_match($filterExpr, $metadata->getTableName()))){
            array_push($newMetadatas, $metadata);
        }        
     }

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

Thanks for the right way

谢谢你的正确方法

回答by Shadi Akil

In addition to above answer, You must have a naming Strategy of your view' entities and the virtual tables, for example: view_your_table, and then you must add the following code to the doctrine.yaml, to disable creating new migration file to the view: schema_filter: ~^(?!view_)~

除了上面的答案,你必须有你的视图实体和虚拟表的命名策略,例如:view_your_table,然后你必须将以下代码添加到doctrine.yaml,以禁止创建新的迁移文件到视图: schema_filter: ~^(?!view_)~