我打算把我的兩分錢在這裏告訴你,根據場只有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
哇非常感謝:)我米要去嘗試一下。對於具有相同問題的其他人:在指定要導入的表的名稱時,您需要在其他情況下寫入名稱,例如,表dbo.tblPlaces必須是「Tblplaces」。如果沒有,它甚至不會生成你70/80%,而不會找到表 – user3746259
你的歡迎。哦,我也經歷過這個相同的問題。我用'[dbo]。[tableName]'。我掙扎了幾天,發現了問題。我什至[posted](http://stackoverflow.com/questions/30105998/weird-alias-0-added-by-doctrine-while-trying-to-run-with-sql-server)關於這一段時間的問題背部。 –
是的,當提到命名時,doctrine和mssql可能會崩潰,我也嘗試了幾個小時來找到問題,直到我有了一個「愚蠢」的想法,只是玩一下表名稱^^ – user3746259