我问了一个类似的问题,但这是一个精炼版本,试图找出问题。
使用symfony 3我遵循此过程来创建表的子类以表示视图。请注意我保持这个人为的例子尽可能简单......
init db:
create database crm;
use crm;
create table customer(customer_id int primary key auto_increment, companyname text);
create view customer_view as select * from customer;
创建一个新的symfony项目:
symfony new crm
编辑parameters.yml:
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: crm
逆向工程元数据XML
php bin/console doctrine:mapping:import --force AppBundle xml
在src/AppBundle/Resources/config/doctrine/CustomerView.orm.xml
手动创建视图映射器:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\CustomerView" table="customer_view">
</entity>
</doctrine-mapping>
生成实体:
php bin/console doctrine:generate:entities AppBundle
手动创建子类实体:
<?php
namespace AppBundle\Entity;
class CustomerView extends Customer
{
}
?>
现在我创建一个只查询视图的控制器:
<?php
namespace AppBundle\Controller;
use ...
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$doc = $this->get ( 'doctrine' );
$repo = $doc->getRepository ( 'AppBundle:CustomerView' );
$result = $repo->createQueryBuilder ( 'c' )->setMaxResults(25)->getQuery ()->getResult ();
return new Response('Result: ' . $result);
}
}
并访问此页面我收到以下异常:
An exception occurred while executing 'SELECT c0_.companyname AS companyname_0, c0_.customer_id AS customer_id_1 FROM customer_view c1_ LIMIT 25':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'c0_.companyname' in 'field list'
如您所见,doctrine为视图使用别名c1
生成了一个查询,但使用别名c0
来选择列!
这是学说中的错误,还是我做了一些不应该支持的事情?
答案 0 :(得分:0)
发生此问题是因为不应在Doctrine中以这种方式使用Entity类继承。 Doctrine认为子类Entites意味着db级继承。应该在CustomerView类中声明customer
中出现的customer_view
的每个字段而不是子类化