2016-03-04 63 views
0

Update1: I've tested this on another project running with mysql - i've deleted one entity and tried to "restore" it by importing the table, I'm getting the same error there as well. So this is not a mssql problem, it must be a config problem. What did I miss to configure?Symfony從MSSQL服務器生成實體[導入]

Update2: When not using the filter, it is importing all and no errors shown. What is wrong with the filter?

我想從現有的mssql數據庫生成實體。爲此,有關的一些信息是什麼我使用:

  • 的Microsoft SQL Server 2012
  • Symfony的2.8與parameters.yml PHP7
  • 驅動程序的數據庫連接:SQLSRV(PDO不MSSQL尚不支持) 。 sqlsrv(不是pdo)的驅動程序可以下載here,我正在使用線程安全版本。

Config.yml:

# Doctrine Configuration 
doctrine: 
    dbal: 
    driver: "%database_driver%" 
    host:  "%database_host%" 
    port:  "%database_port%" 
    dbname: "%database_name%" 
    user:  "%database_user%" 
    password: "%database_password%" 

    orm: 
    default_entity_manager: default 
    auto_generate_proxy_classes: "%kernel.debug%" 
    entity_managers: 
     default: 
     auto_mapping: true 
     naming_strategy: doctrine.orm.naming_strategy.underscore 

連接到與DBAL部分正在工作的規定值的數據庫,我可以在我的代碼和運行架構更新命令創建一個新的實體創建相應的表。在另一方面,當試圖從現有的表的實體 - 它被命名爲「dbo.tblPlaces」 - 使用下面的命令:

php app/console doctrine:mapping:import --force AppBundle xml --filter="tblPlaces" 

(我也試過內的「dbo.tblPlaces」過濾器),我不斷收到錯誤:

Database does not have any mapping information.

我讀過幾乎所有其他文章中,我能找到這裏計算器和有關上述錯誤的symfony的正式文件,但沒有能夠找到一個解決方案。爲什麼我不斷收到這個煩人的錯誤信息?該表是非常小的,只包括以下字段:

tbl.Places

回答

1

我打算把我的兩分錢在這裏告訴你,根據場只有70-80%的必要的映射信息,索引和外鍵約束。

Docs

Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events

我面臨着MSSQL和Symfony的而回同樣的問題。

我結束了一個替代解決方案。

我最終結果是我在MsSQL中創建了一個存儲過程,它以記錄的形式在SQL服務器中生成實體。然後,您可以將其複製並粘貼到您的實體文件中,您將能夠生成工作實體。它對我來說非常完美,對你也有希望。

你可以從這裏https://github.com/dipens/W3_APP_GEN_DOCTRINE.sql/blob/master/W3_APP_GEN_Doctrine

代碼在這裏下載:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 


ALTER PROCEDURE [dbo].[W3_APP_GEN_Doctrine] @tablename varchar(255) 
AS 

WITH D AS (
select data_type + 
    case 
     when data_type like '%text' or data_type like 'image' or data_type like 'sql_variant' or data_type like 'xml' 
      then '' 
     when data_type = 'float' 
      then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ')' 
     when data_type = 'numeric' or data_type = 'decimal' 
      then '(' + convert(varchar(10), isnull(numeric_precision, 18)) + ',' + convert(varchar(10), isnull(numeric_scale, 0)) + ')' 
     when (data_type like '%char' or data_type like '%binary') and character_maximum_length = -1 
      then '(max)' 
     when character_maximum_length is not null 
      then '(' + convert(varchar(10), character_maximum_length) + ')' 
     else '' 
    end as CONDENSED_TYPE 
    , 

    case 
     when data_type like '%text%' or data_type like '%char%' OR data_type ='uniqueidentifier' OR data_type ='bigint' 
      then 'string' 
     when data_type ='int' or data_type ='smallint' or data_type ='tinyint' 
      then 'integer' 
     when data_type ='date' 
      then 'datetime' 
     when data_type ='bit' 
      then 'boolean' 
     when data_type ='decimal' 
      then 'string' --'decimal' 
     when data_type ='datetime' 
      then data_type 
     when data_type ='double' OR data_type ='float' 
      then 'float' 
     when data_type ='money' 
      then 'string' --'decimal' 
     when data_type ='bigint' 
      then 'string' 
    end as DoctrineType 


    ,object_id(table_name) AS TID ,C.*, SC.is_computed, SC.is_identity 
from information_schema.columns C 
INNER JOIN sys.columns SC ON object_id(table_name)=SC.object_id and C.COLUMN_NAME=SC.name 
where table_name= @tablename 

) 
SELECT 
'/**' 
+ CHAR(13)+CHAR(10) + '* @var ' + IIF(D.DoctrineType='datetime','\'+D.DoctrineType,D.DoctrineType) 
+ CHAR(13)+CHAR(10) + '*' 
+ CHAR(13)+CHAR(10) + '* @ORM\Column(name="' + D.COLUMN_NAME + '", type="' + D.DoctrineType + '"' 
    + IIF(D.DoctrineType='string' and D.DATA_TYPE NOT IN ('uniqueidentifier','decimal','money','bigint'),', length=' + convert(varchar(25),D.CHARACTER_MAXIMUM_LENGTH),'') 
    + IIF(D.DoctrineType='decimal',', precision=' + convert(varchar(25),D.NUMERIC_PRECISION) + ', scale=' + convert(varchar(25),D.NUMERIC_SCALE),'') 
    + IIF(D.IS_NULLABLE='NO' and D.is_identity=0,', nullable=false','') 
    + ')' 
+ IIF(D.is_identity=1,CHAR(13)+CHAR(10) + '* @ORM\Id','') 
+ IIF(D.is_identity=1,CHAR(13)+CHAR(10) + '* @ORM\GeneratedValue(strategy="AUTO")','') 
+ CHAR(13)+CHAR(10) + '*/' 
+ CHAR(13)+CHAR(10) 
+ CHAR(13)+CHAR(10) + 'private $' + D.COLUMN_NAME +';' 
+ CHAR(13)+CHAR(10) 

AS DOCTRINE 
--,D.ORDINAL_POSITION 
FROM D 
--ORDER BY D.ORDINAL_POSITION 

UNION ALL 

SELECT 
'/**' 
+ CHAR(13)+CHAR(10) + '* @return ' + IIF(D.DoctrineType='datetime','\'+D.DoctrineType,D.DoctrineType) 
+ CHAR(13)+CHAR(10) + '*/' 
+ CHAR(13)+CHAR(10) + 'public function get' + replace(D.COLUMN_NAME,'_','') + '()' 
+ CHAR(13)+CHAR(10) + '{' 
+ CHAR(13)+CHAR(10) + ' return $this->' + D.COLUMN_NAME + ';' 
+ CHAR(13)+CHAR(10) + '}' 
+ CHAR(13)+CHAR(10) 
+ CHAR(13)+CHAR(10)+ '/**' 
+ CHAR(13)+CHAR(10) + '* @param ' + IIF(D.DoctrineType='datetime','\'+D.DoctrineType,D.DoctrineType) + ' $' + D.COLUMN_NAME 
+ CHAR(13)+CHAR(10) + '*/' 
+ CHAR(13)+CHAR(10) + 'public function set' + replace(D.COLUMN_NAME,'_','') + '('+ '$' + D.COLUMN_NAME + ')' 
+ CHAR(13)+CHAR(10) + '{' 
+ CHAR(13)+CHAR(10) + ' $this->' + D.COLUMN_NAME + '=' + '$' + D.COLUMN_NAME + ';' 
+ CHAR(13)+CHAR(10) + '}' 
+ CHAR(13)+CHAR(10) 

--,D.ORDINAL_POSITION 
FROM D 
+0

哇非常感謝:)我米要去嘗試一下。對於具有相同問題的其他人:在指定要導入的表的名稱時,您需要在其他情況下寫入名稱,例如,表dbo.tblPlaces必須是「Tblplaces」。如果沒有,它甚至不會生成你70/80%,而不會找到表 – user3746259

+1

你的歡迎。哦,我也經歷過這個相同的問題。我用'[dbo]。[tableName]'。我掙扎了幾天,發現了問題。我什至[posted](http://stackoverflow.com/questions/30105998/weird-alias-0-added-by-doctrine-while-trying-to-run-with-sql-server)關於這一段時間的問題背部。 –

+0

是的,當提到命名時,doctrine和mssql可能會崩潰,我也嘗試了幾個小時來找到問題,直到我有了一個「愚蠢」的想法,只是玩一下表名稱^^ – user3746259